Skip to content

Commit

Permalink
fix: Fix Importing Illustrations when unpack web archives is disabled…
Browse files Browse the repository at this point in the history
… - MEED-7091 - Meeds-io/meeds#2196 (#124)

Prior to this change, when setting variable in , the portlet instances
and page templates aren't imported properly. This change ensures to
import default portlet images and page templates illustrations from an
absolute file path outside of archives.
  • Loading branch information
boubaker authored Jun 18, 2024
1 parent 4c55769 commit 7e311a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -228,14 +230,15 @@ protected PageTemplate createPageTemplate(PageTemplateDescriptor d, long oldTemp
}

protected void saveIllustration(long pageTemplateId, String imagePath) {
File tempFile = null;
try {
URL resource = configurationManager.getResource(imagePath);
tempFile = getIllustrationFile(imagePath);
String uploadId = "PageTemplateIllustration" + RANDOM.nextLong();
UploadResource uploadResource = new UploadResource(uploadId);
uploadResource.setFileName(new File(resource.getPath()).getName());
uploadResource.setFileName(tempFile.getName());
uploadResource.setMimeType("image/png");
uploadResource.setStatus(UploadResource.UPLOADED_STATUS);
uploadResource.setStoreLocation(resource.getPath());
uploadResource.setStoreLocation(tempFile.getPath());
UploadedAttachmentDetail uploadedAttachmentDetail = new UploadedAttachmentDetail(uploadResource);
attachmentService.deleteAttachments(PageTemplateAttachmentPlugin.OBJECT_TYPE, String.valueOf(pageTemplateId));
attachmentService.saveAttachment(uploadedAttachmentDetail,
Expand All @@ -248,6 +251,14 @@ protected void saveIllustration(long pageTemplateId, String imagePath) {
imagePath,
pageTemplateId),
e);
} finally {
if (tempFile != null) {
try {
Files.delete(tempFile.toPath());
} catch (IOException e) {
tempFile.deleteOnExit();
}
}
}
}

Expand All @@ -273,4 +284,12 @@ protected long getSettingValue(String name) {
}
}

private File getIllustrationFile(String imagePath) throws Exception {
try (InputStream inputStream = configurationManager.getInputStream(imagePath)) {
File tempFile = File.createTempFile("temp", ".png");
FileUtils.copyInputStreamToFile(inputStream, tempFile);
return tempFile;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -310,14 +312,15 @@ protected PortletInstance savePortletInstance(PortletInstanceDescriptor d, long
}

protected void saveIllustration(long portletInstanceId, String imagePath) {
File tempFile = null;
try {
URL resource = configurationManager.getResource(imagePath);
tempFile = getIllustrationFile(imagePath);
String uploadId = "PortletInstanceIllustration" + RANDOM.nextLong();
UploadResource uploadResource = new UploadResource(uploadId);
uploadResource.setFileName(new File(resource.getPath()).getName());
uploadResource.setFileName(tempFile.getName());
uploadResource.setMimeType("image/png");
uploadResource.setStatus(UploadResource.UPLOADED_STATUS);
uploadResource.setStoreLocation(resource.getPath());
uploadResource.setStoreLocation(tempFile.getPath());
UploadedAttachmentDetail uploadedAttachmentDetail = new UploadedAttachmentDetail(uploadResource);
attachmentService.deleteAttachments(PortletInstanceAttachmentPlugin.OBJECT_TYPE, String.valueOf(portletInstanceId));
attachmentService.saveAttachment(uploadedAttachmentDetail,
Expand All @@ -330,6 +333,14 @@ protected void saveIllustration(long portletInstanceId, String imagePath) {
imagePath,
portletInstanceId),
e);
} finally {
if (tempFile != null) {
try {
Files.delete(tempFile.toPath());
} catch (IOException e) {
tempFile.deleteOnExit();
}
}
}
}

Expand Down Expand Up @@ -365,6 +376,14 @@ protected long getSettingValue(String name) {
}
}

private File getIllustrationFile(String imagePath) throws Exception {
try (InputStream inputStream = configurationManager.getInputStream(imagePath)) {
File tempFile = File.createTempFile("temp", ".png");
FileUtils.copyInputStreamToFile(inputStream, tempFile);
return tempFile;
}
}

private ClassLoader getClassLoader() {
return getClass().getClassLoader();
}
Expand Down

0 comments on commit 7e311a2

Please sign in to comment.