Skip to content

Commit

Permalink
feat: Import Logo and Favicon in first startup - Meeds-io/MIPs#134
Browse files Browse the repository at this point in the history
Prior to this change, the Logog and Favicon aren't imported but read directly from sources each startup which makes it difficult to reuse in other modules as fileItem using a single store (FileService). This change enures to import those files the first server startup with those files.
  • Loading branch information
boubaker committed Jul 26, 2024
1 parent d8cd255 commit f8322a3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.exoplatform.portal.branding;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -69,6 +70,8 @@
import org.exoplatform.upload.UploadResource;
import org.exoplatform.upload.UploadService;

import lombok.SneakyThrows;

@SuppressWarnings("unchecked")
public class BrandingServiceImpl implements BrandingService, Startable {

Expand Down Expand Up @@ -461,7 +464,7 @@ public Logo getLogo() {
if (imageId != null && imageId > 0) {
this.logo = retrieveStoredBrandingFile(imageId, new Logo());
} else {
this.logo = retrieveDefaultBrandingFile(defaultConfiguredLogoPath, new Logo());
this.logo = retrieveDefaultBrandingFile(defaultConfiguredLogoPath, new Logo(), LOGO_NAME, BRANDING_LOGO_ID_SETTING_KEY);
}
} catch (Exception e) {
LOG.warn("Error retrieving logo", e);
Expand All @@ -478,7 +481,7 @@ public Favicon getFavicon() {
if (imageId != null) {
this.favicon = retrieveStoredBrandingFile(imageId, new Favicon());
} else {
this.favicon = retrieveDefaultBrandingFile(defaultConfiguredFaviconPath, new Favicon());
this.favicon = retrieveDefaultBrandingFile(defaultConfiguredFaviconPath, new Favicon(), FAVICON_NAME, BRANDING_FAVICON_ID_SETTING_KEY);
}
} catch (Exception e) {
LOG.warn("Error retrieving favicon", e);
Expand All @@ -495,7 +498,7 @@ public Background getLoginBackground() {
if (imageId != null) {
this.loginBackground = retrieveStoredBrandingFile(imageId, new Background());
} else if (StringUtils.isNotBlank(defaultConfiguredLoginBgPath)) {
this.loginBackground = retrieveDefaultBrandingFile(defaultConfiguredLoginBgPath, new Background());
this.loginBackground = retrieveDefaultBrandingFile(defaultConfiguredLoginBgPath, new Background(), LOGIN_BACKGROUND_NAME, BRANDING_LOGIN_BG_ID_SETTING_KEY);
} else {
this.loginBackground = new Background();
}
Expand Down Expand Up @@ -919,14 +922,20 @@ private void removeBrandingFile(Long fileId, String settingKey) {
}
}

@SneakyThrows
private void updateBrandingFileByUploadId(String uploadId,
String fileName,
String settingKey) throws Exception {
String settingKey) {
InputStream inputStream = getUploadDataAsStream(uploadId);
if (inputStream == null) {
throw new IllegalArgumentException("Cannot update " + fileName +
", the object must contain the image data or an upload id");
}
updateBrandingFileByInputStream(inputStream, fileName, settingKey);
}

@SneakyThrows
private FileItem updateBrandingFileByInputStream(InputStream inputStream, String fileName, String settingKey) {
int size = inputStream.available();
FileItem fileItem = new FileItem(0l,
fileName,
Expand All @@ -942,6 +951,7 @@ private void updateBrandingFileByUploadId(String uploadId,
Scope.GLOBAL,
settingKey,
SettingValue.create(String.valueOf(fileItem.getFileInfo().getId())));
return fileItem;
}

private String computeThemeCSS() {// NOSONAR
Expand Down Expand Up @@ -1029,22 +1039,28 @@ private <T extends BrandingFile> T retrieveStoredBrandingFile(long imageId, T br
return brandingFile;
}

private <T extends BrandingFile> T retrieveDefaultBrandingFile(String imagePath, T brandingFile) throws IOException {
private <T extends BrandingFile> T retrieveDefaultBrandingFile(String imagePath, T brandingFile, String fileName, String settingKey) throws IOException {
if (StringUtils.isNotBlank(imagePath)) {
byte[] bytes = null;
long lastModified = DEFAULT_LAST_MODIFED;
File file = new File(imagePath);
if (file.exists()) {
brandingFile.setData(Files.readAllBytes(file.toPath()));
brandingFile.setSize(file.length());
brandingFile.setUpdatedDate(file.lastModified());
bytes = Files.readAllBytes(file.toPath());
lastModified = file.lastModified();
} else {
InputStream is = container.getPortalContext().getResourceAsStream(imagePath);
if (is != null) {
byte[] streamContentAsBytes = IOUtil.getStreamContentAsBytes(is);
brandingFile.setData(streamContentAsBytes);
brandingFile.setSize(streamContentAsBytes.length);
brandingFile.setUpdatedDate(DEFAULT_LAST_MODIFED);
try (InputStream is = container.getPortalContext().getResourceAsStream(imagePath)) {
if (is != null) {
bytes = IOUtil.getStreamContentAsBytes(is);
}
}
}
if (bytes != null) {
FileItem fileItem = updateBrandingFileByInputStream(new ByteArrayInputStream(bytes), fileName, settingKey);
brandingFile.setFileId(fileItem.getFileInfo().getId());
brandingFile.setData(bytes);
brandingFile.setSize(bytes.length);
brandingFile.setUpdatedDate(lastModified);
}
}
return brandingFile;
}
Expand Down Expand Up @@ -1120,4 +1136,4 @@ private ExoFeatureService getFeatureService() {
return featureService;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -61,6 +62,7 @@
import org.exoplatform.commons.file.model.FileInfo;
import org.exoplatform.commons.file.model.FileItem;
import org.exoplatform.commons.file.services.FileService;
import org.exoplatform.commons.file.services.FileStorageException;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.configuration.ConfigurationManager;
import org.exoplatform.container.xml.InitParams;
Expand Down Expand Up @@ -314,7 +316,7 @@ public void shouldGetUpdatedBrandingInformationWhenInformationUpdated() {
}

@Test
public void shouldGetBrandingInformationWithoutBinaries() {
public void shouldGetBrandingInformationWithoutBinaries() throws FileStorageException, IOException {

// Given
SettingService settingService = mock(SettingService.class);
Expand Down Expand Up @@ -365,6 +367,7 @@ public void shouldGetBrandingInformationWithoutBinaries() {
1, 2, 3
}));

when(fileService.writeFile(any())).thenAnswer(invocation -> invocation.getArgument(0));
assertNotNull(brandingService.getLoginBackgroundPath());
assertNotNull(brandingService.getLogoPath());
assertNotNull(brandingService.getLoginBackgroundPath());
Expand Down Expand Up @@ -683,6 +686,7 @@ public void testGetDefaultLoginBackground() throws Exception {
when(context.getResourceAsStream(imagePath)).thenReturn(new ByteArrayInputStream(new byte[] {
1, 2, 3
}));
when(fileService.writeFile(any())).thenAnswer(invocation -> invocation.getArgument(0));
Background loginBackground = brandingService.getLoginBackground();
assertNotNull(loginBackground);
assertEquals(3, loginBackground.getSize());
Expand Down Expand Up @@ -730,6 +734,7 @@ public void testGetDefaultFavicon() throws Exception {
when(context.getResourceAsStream(imagePath)).thenReturn(new ByteArrayInputStream(new byte[] {
1, 2, 3
}));
when(fileService.writeFile(any())).thenAnswer(invocation -> invocation.getArgument(0));
Favicon favicon = brandingService.getFavicon();
assertNotNull(favicon);
assertEquals(3, favicon.getSize());
Expand Down Expand Up @@ -777,6 +782,7 @@ public void testGetDefaultLogo() throws Exception {
when(context.getResourceAsStream(imagePath)).thenReturn(new ByteArrayInputStream(new byte[] {
1, 2, 3
}));
when(fileService.writeFile(any())).thenAnswer(invocation -> invocation.getArgument(0));
Logo logo = brandingService.getLogo();
assertNotNull(logo);
assertEquals(3, logo.getSize());
Expand Down

0 comments on commit f8322a3

Please sign in to comment.