Commit 78f9c34d authored by Martin Lowe's avatar Martin Lowe 🇨🇦
Browse files

Merge branch 'malowe/dev/425' into 'dev'

Update image service to optionally save back to the eclipseDB

See merge request !438
parents 701594cd bda9c8d6
Pipeline #1513 passed with stage
in 0 seconds
......@@ -68,18 +68,21 @@ public interface ImageStoreService {
*
*/
@ConfigMapping(prefix = "eclipse.image-store")
public interface ImageSeriveConfig {
public interface ImageServiceConfig {
String filePath();
String webRoot();
@WithDefault("1000000")
@WithDefault("65000")
long maxSizeInBytes();
@WithDefault("")
String defaultImageUrl();
Compression compression();
@WithDefault("false")
boolean persistToDb();
/**
* Represents compression configuration settings.
......
......@@ -21,6 +21,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
......@@ -40,6 +41,12 @@ import javax.ws.rs.BadRequestException;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.eclipsefoundation.core.model.RequestWrapper;
import org.eclipsefoundation.eclipsedb.dao.EclipseDBPersistenceDAO;
import org.eclipsefoundation.eclipsedb.dto.OrganizationInformation;
import org.eclipsefoundation.persistence.model.RDBMSQuery;
import org.eclipsefoundation.persistence.service.FilterService;
import org.eclipsefoundation.react.helper.ImageFileHelper;
import org.eclipsefoundation.react.namespace.ImageStoreFormat;
import org.eclipsefoundation.react.service.ImageStoreService;
......@@ -61,7 +68,11 @@ public class DefaultImageStoreService implements ImageStoreService {
public static final Logger LOGGER = LoggerFactory.getLogger(DefaultImageStoreService.class);
@Inject
ImageSeriveConfig config;
ImageServiceConfig config;
@Inject
EclipseDBPersistenceDAO dao;
@Inject
FilterService filters;
// local copy of image store location
private Path imageStoreRoot;
......@@ -138,11 +149,27 @@ public class DefaultImageStoreService implements ImageStoreService {
format);
// cannot provide image, return null
return null;
} else if (bytes.length > config.maxSizeInBytes()
&& !approximatelyMatch((long) bytes.length, attrView.readAttributes().size(), 1000)) {
} else if (bytes.length > config.maxSizeInBytes() && (!Files.exists(p)
|| !approximatelyMatch((long) bytes.length, attrView.readAttributes().size(), 1000))) {
throw new BadRequestException(
"Passed image is larger than allowed size of '" + config.maxSizeInBytes() + "' bytes");
}
// if enabled, update the EclipseDB on logo update when image name is numeric
// TODO remove once the Drupal API references this API as this will no longer be needed then
if (config.persistToDb() && format.isPresent()
&& ImageStoreFormat.ImageStoreFormats.WEB.equals(format.get()) && StringUtils.isNumeric(fileName)) {
// get a ref to the given organization information object
OrganizationInformation oi = dao.getReference(Integer.valueOf(fileName), OrganizationInformation.class);
// as long as this org exists, update the logo
if (oi != null) {
oi.setLargeLogo(bytes);
oi.setSmallLogo(bytes);
oi.setLargeMime(mimeType);
oi.setSmallMime(mimeType);
dao.add(new RDBMSQuery<>(new RequestWrapper(), filters.get(OrganizationInformation.class)), Arrays.asList(oi));
}
}
// write will create and overwrite file by default if it exists
return getWebUrl(Files.write(p, compress(bytes, mimeType)));
} catch (IOException e) {
......@@ -248,7 +275,7 @@ public class DefaultImageStoreService implements ImageStoreService {
writer.write(null, new IIOImage(image, null, null), param);
return os.toByteArray();
} catch (IOException e) {
LOGGER.warn("Unable to compress image with mime type of '{}', returning original bytes",mimeType, e);
LOGGER.warn("Unable to compress image with mime type of '{}', returning original bytes", mimeType, e);
return imageBytes;
} finally {
// close the writer once done (not autoclosable)
......
......@@ -93,6 +93,7 @@ eclipse.image-store.default-image-url=https://www.eclipse.org/eclipse.org-common
%dev.eclipse.image-store.file-path=/home/martin/localdev/membership.eclipse.org/volumes/imagestore
%dev.eclipse.mailer.contact-removal.to=webdev@eclipse-foundation.org
%dev.eclipse.scheduled.membership.enabled=false
%dev.eclipse.image-store.persist-to-db=true
## Optional dev settings
#%dev.quarkus.hibernate-orm.database.generation=drop-and-create
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment