This is an automated email from the ASF dual-hosted git repository.

dklco pushed a commit to branch dklco/preview-img
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git


The following commit(s) were added to refs/heads/dklco/preview-img by this push:
     new 2e8258c  Adding missing files
2e8258c is described below

commit 2e8258cb8a58548be97003da08e2d2f2e7197725
Author: Dan Klco <[email protected]>
AuthorDate: Thu Mar 11 22:01:25 2021 -0500

    Adding missing files
---
 .../jobs/FileMetadataExtractorConsumer.java        | 73 ++++++++++++++++++++
 .../core/internal/servlets/PreviewFileServlet.java | 77 ++++++++++++++++++++++
 .../libs/sling-cms/content/file/preview.json       |  4 ++
 3 files changed, 154 insertions(+)

diff --git 
a/core/src/main/java/org/apache/sling/cms/core/internal/jobs/FileMetadataExtractorConsumer.java
 
b/core/src/main/java/org/apache/sling/cms/core/internal/jobs/FileMetadataExtractorConsumer.java
new file mode 100644
index 0000000..cf81484
--- /dev/null
+++ 
b/core/src/main/java/org/apache/sling/cms/core/internal/jobs/FileMetadataExtractorConsumer.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.cms.core.internal.jobs;
+
+import java.io.IOException;
+import java.util.Collections;
+
+import org.apache.sling.api.SlingConstants;
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.cms.File;
+import org.apache.sling.cms.FileMetadataExtractor;
+import org.apache.sling.event.jobs.Job;
+import org.apache.sling.event.jobs.consumer.JobConsumer;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Sling Job Consumer for extracting the metadata from a file
+ */
+@Component(service = { JobConsumer.class }, property = {
+        JobConsumer.PROPERTY_TOPICS + "=" + 
FileMetadataExtractorConsumer.TOPIC })
+public class FileMetadataExtractorConsumer implements JobConsumer {
+
+    public static final Logger log = 
LoggerFactory.getLogger(FileMetadataExtractorConsumer.class);
+
+    public static final String TOPIC = "org/apache/sling/cms/ExtractMetadata";
+
+    @Reference
+    private FileMetadataExtractor extractor;
+
+    @Reference
+    private ResourceResolverFactory factory;
+
+    @Override
+    public JobResult process(Job job) {
+        String path = job.getProperty(SlingConstants.PROPERTY_PATH, 
String.class);
+        try (ResourceResolver serviceResolver = 
factory.getServiceResourceResolver(
+                Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, 
"sling-cms-metadata"))) {
+            log.debug("Processing metadata for {}", path);
+            Resource resource = serviceResolver.getResource(path);
+            File file = resource.adaptTo(File.class);
+            log.debug("Retrieved file {}", file);
+            extractor.updateMetadata(file);
+            log.debug("Metadata extracted successfully");
+            return JobResult.OK;
+        } catch (LoginException e) {
+            log.error("Exception getting service user", e);
+        } catch (IOException e) {
+            log.error("Failed to extract metadata from {}", path, e);
+        }
+        return JobResult.FAILED;
+    }
+
+}
diff --git 
a/core/src/main/java/org/apache/sling/cms/core/internal/servlets/PreviewFileServlet.java
 
b/core/src/main/java/org/apache/sling/cms/core/internal/servlets/PreviewFileServlet.java
new file mode 100644
index 0000000..516744d
--- /dev/null
+++ 
b/core/src/main/java/org/apache/sling/cms/core/internal/servlets/PreviewFileServlet.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.cms.core.internal.servlets;
+
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.osgi.service.component.annotations.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(service = Servlet.class, property = { 
"sling.servlet.resourceTypes=sling-cms/file/preview",
+        "sling.servlet.methods=GET" })
+public class PreviewFileServlet extends SlingSafeMethodsServlet {
+
+    private static final long serialVersionUID = 6234007100684499058L;
+
+    private static final Logger log = 
LoggerFactory.getLogger(PreviewFileServlet.class);
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, 
SlingHttpServletResponse response)
+            throws ServletException, IOException {
+        String suffix = request.getRequestPathInfo().getSuffix();
+
+        log.debug("Getting thumbnail for {}", suffix);
+        Pattern p = Pattern.compile("([a-z\\-\\/]+)\\.([a-z]+)(\\/.+)?");
+        Matcher matcher = p.matcher(suffix);
+
+        if (matcher.matches()) {
+            String path = matcher.group(1);
+            String extension = matcher.group(2);
+            String newSuffix = "";
+            if (matcher.groupCount() >= 4 && matcher.group(3) != null) {
+                newSuffix = matcher.group(3);
+            }
+            log.debug("Loaded path={}, extension={}, suffix={}", path, 
extension, newSuffix);
+
+            RequestDispatcherOptions options = new RequestDispatcherOptions();
+            options.setReplaceSuffix(newSuffix);
+            options.setReplaceSelectors("");
+
+            Resource resource = request.getResourceResolver().getResource(path 
+ "." + extension);
+            log.debug("getResolutionPathInfo={}", 
resource.getResourceMetadata().getResolutionPathInfo());
+            RequestDispatcher dispatcher = 
request.getRequestDispatcher(resource, options);
+
+            dispatcher.forward(request, response);
+        } else {
+            throw new ServletException("Unexpected suffix pattern");
+        }
+
+    }
+
+}
diff --git 
a/ui/src/main/resources/jcr_root/libs/sling-cms/content/file/preview.json 
b/ui/src/main/resources/jcr_root/libs/sling-cms/content/file/preview.json
new file mode 100644
index 0000000..6ed22f9
--- /dev/null
+++ b/ui/src/main/resources/jcr_root/libs/sling-cms/content/file/preview.json
@@ -0,0 +1,4 @@
+{
+    "jcr:primaryType": "nt:unstructured",
+    "sling:resourceType": "sling-cms/file/preview"
+}

Reply via email to