Skip to content

Commit f822f7c

Browse files
committed
feat: Implement PWA Settings Admin UI - MEED-7252 - Meeds-io/MIPs#134
1 parent 10d4669 commit f822f7c

File tree

26 files changed

+692
-124
lines changed

26 files changed

+692
-124
lines changed

component/api/src/main/java/org/exoplatform/social/metadata/thumbnail/ImageThumbnailService.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

component/core/src/main/java/org/exoplatform/social/core/attachment/AttachmentServiceImpl.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ public ObjectAttachmentOperationReport saveAttachments(FileAttachmentResourceLis
115115
metadataObject);
116116
ObjectAttachmentOperationReport report = null;
117117
List<FileAttachmentObject> remainingFiles =
118-
CollectionUtils.isEmpty(attachmentList.getAttachedFiles()) ? Collections.emptyList()
119-
: attachmentList.getAttachedFiles();
118+
CollectionUtils.isEmpty(attachmentList.getAttachedFiles()) ?
119+
Collections.emptyList() :
120+
attachmentList.getAttachedFiles();
120121
if (CollectionUtils.isNotEmpty(existingAttachments)) {
121122

122123
List<String> remainingFileIds = remainingFiles.stream().map(file -> file.getId()).distinct().toList();
@@ -281,15 +282,26 @@ public InputStream getAttachmentInputStream(String objectType,
281282
String objectId,
282283
String fileId,
283284
String imageDimensions,
284-
Identity userAclIdentity) throws ObjectNotFoundException, IllegalAccessException,
285+
Identity userAclIdentity) throws ObjectNotFoundException,
286+
IllegalAccessException,
285287
IOException {
286-
org.exoplatform.social.core.identity.model.Identity userIdentity = checkAccessPermission(objectType,
287-
objectId,
288-
fileId,
289-
userAclIdentity);
288+
if (objectType == null) {
289+
throw new IllegalArgumentException("objectType is mandatory");
290+
}
291+
if (objectId == null) {
292+
throw new IllegalArgumentException("objectId is mandatory");
293+
}
294+
if (fileId == null) {
295+
throw new IllegalArgumentException("fileId is mandatory");
296+
}
297+
if (!hasAccessPermission(userAclIdentity, objectType, objectId)) {
298+
throw new IllegalAccessException(String.format("User %s doesn't have enough permissions to get attached files on object %s/%s",
299+
userAclIdentity.getUserId(),
300+
objectType,
301+
objectId));
302+
}
290303
return attachmentStorage.getAttachmentInputStream(new ObjectAttachmentId(fileId, objectType, objectId),
291-
imageDimensions,
292-
userIdentity);
304+
imageDimensions);
293305
}
294306

295307
@Override
@@ -316,7 +328,8 @@ private org.exoplatform.social.core.identity.model.Identity checkAccessPermissio
316328
return checkAccessPermission(objectType, objectId, userAclIdentity);
317329
}
318330

319-
private org.exoplatform.social.core.identity.model.Identity checkAccessPermission(String objectType, String objectId,
331+
private org.exoplatform.social.core.identity.model.Identity checkAccessPermission(String objectType,
332+
String objectId,
320333
Identity userAclIdentity) throws ObjectNotFoundException,
321334
IllegalAccessException {
322335
if (StringUtils.isBlank(objectType)) {
@@ -367,8 +380,10 @@ private void checkEditPermissions(FileAttachmentResourceList attachmentList,
367380
}
368381

369382
if (!hasEditPermission(userAclIdentity, objectType, objectId)) {
370-
throw new IllegalAccessException("User " + userAclIdentity.getUserId()
371-
+ " doesn't have enough permissions to update file attachments of object " + objectType + "/" + objectId);
383+
throw new IllegalAccessException(String.format("User %s doesn't have enough permissions to update file attachments of object %s/%s",
384+
userAclIdentity.getUserId(),
385+
objectType,
386+
objectId));
372387
}
373388
}
374389

@@ -436,7 +451,7 @@ private void createAttachment(String fileId,
436451
String objectId,
437452
String parentObjectId,
438453
long userIdentityId,
439-
Map<String, String> properties) throws ObjectNotFoundException, ObjectAlreadyExistsException {
454+
Map<String, String> properties) throws ObjectNotFoundException, ObjectAlreadyExistsException {
440455
MetadataKey metadataKey = null;
441456
metadataKey = new MetadataKey(METADATA_TYPE.getName(), fileId, getAudienceId(objectType, objectId));
442457
MetadataObject object = new MetadataObject(objectType,
@@ -535,8 +550,10 @@ private void broadcastAttachmentsChange(String eventName,
535550

536551
private String getUserName(long userIdentityId) {
537552
org.exoplatform.social.core.identity.model.Identity identity =
538-
userIdentityId > 0 ? identityManager.getIdentity(String.valueOf(userIdentityId))
539-
: null;
553+
userIdentityId
554+
> 0 ?
555+
identityManager.getIdentity(String.valueOf(userIdentityId)) :
556+
null;
540557
return identity == null ? null : identity.getRemoteId();
541558
}
542559

component/core/src/main/java/org/exoplatform/social/core/attachment/storage/FileAttachmentStorage.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@
2929
import org.exoplatform.commons.file.services.FileStorageException;
3030
import org.exoplatform.services.log.ExoLogger;
3131
import org.exoplatform.services.log.Log;
32+
import org.exoplatform.services.thumbnail.ImageThumbnailService;
3233
import org.exoplatform.social.attachment.model.ObjectAttachmentDetail;
3334
import org.exoplatform.social.attachment.model.ObjectAttachmentId;
3435
import org.exoplatform.social.common.Utils;
35-
import org.exoplatform.social.core.identity.model.Identity;
36-
import org.exoplatform.social.metadata.thumbnail.ImageThumbnailService;
3736

3837
public class FileAttachmentStorage {
3938

@@ -94,9 +93,7 @@ public ObjectAttachmentDetail getAttachment(ObjectAttachmentId attachmentId) {
9493
return null;
9594
}
9695

97-
public InputStream getAttachmentInputStream(ObjectAttachmentId attachmentId,
98-
String imageDimensions,
99-
Identity userIdentity) throws IOException {
96+
public InputStream getAttachmentInputStream(ObjectAttachmentId attachmentId, String imageDimensions) throws IOException {
10097
long fileId = Long.parseLong(attachmentId.getFileId());
10198
FileInfo fileInfo = fileService.getFileInfo(fileId);
10299
if (fileInfo == null) {
@@ -112,7 +109,6 @@ public InputStream getAttachmentInputStream(ObjectAttachmentId attachmentId,
112109
int[] dimension = Utils.parseDimension(imageDimensions);
113110
try {
114111
FileItem imageFileItem = imageThumbnailService.getOrCreateThumbnail(fileItem,
115-
userIdentity,
116112
dimension[0],
117113
dimension[1]);
118114
return imageFileItem.getAsStream();

component/core/src/main/java/org/exoplatform/social/core/listeners/ThumbnailFileListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.exoplatform.commons.file.model.FileInfo;
1919
import org.exoplatform.services.listener.Event;
2020
import org.exoplatform.services.listener.Listener;
21-
import org.exoplatform.social.metadata.thumbnail.ImageThumbnailService;
21+
import org.exoplatform.services.thumbnail.ImageThumbnailService;
2222

2323
public class ThumbnailFileListener extends Listener<FileInfo, Object> {
2424

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* along with this program; if not, write to the Free Software Foundation,
1414
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1515
*/
16-
package org.exoplatform.social.core.metadata.thumbnail;
16+
package org.exoplatform.social.core.thumbnail;
1717

1818
import org.apache.commons.io.IOUtils;
1919
import org.exoplatform.commons.file.model.FileInfo;
@@ -23,12 +23,11 @@
2323
import org.exoplatform.services.log.ExoLogger;
2424
import org.exoplatform.services.log.Log;
2525
import org.exoplatform.services.thumbnail.ImageResizeService;
26-
import org.exoplatform.social.core.identity.model.Identity;
26+
import org.exoplatform.services.thumbnail.ImageThumbnailService;
2727
import org.exoplatform.social.metadata.MetadataService;
2828
import org.exoplatform.social.metadata.model.MetadataItem;
2929
import org.exoplatform.social.metadata.model.MetadataKey;
3030
import org.exoplatform.social.metadata.model.MetadataType;
31-
import org.exoplatform.social.metadata.thumbnail.ImageThumbnailService;
3231
import org.exoplatform.social.metadata.thumbnail.model.ThumbnailObject;
3332

3433
import java.io.ByteArrayInputStream;
@@ -69,36 +68,50 @@ public ImageThumbnailServiceImpl(MetadataService metadataService,
6968
* {@inheritDoc}
7069
*/
7170
@Override
72-
public FileItem getOrCreateThumbnail(FileItem file, Identity identity, int width, int height) throws Exception {
71+
public FileItem getOrCreateThumbnail(FileItem file, int width, int height) throws Exception {
72+
return getOrCreateThumbnail(null, file, width, height);
73+
}
74+
75+
@Override
76+
public FileItem getOrCreateThumbnail(ImageResizeService resizeSupplier,
77+
FileItem file,
78+
int width,
79+
int height) throws Exception {
7380
if (file == null) {
7481
throw new IllegalArgumentException("file argument is mandatory");
7582
}
7683
if (width == 0 && height == 0) {
7784
return file;
7885
}
86+
if (resizeSupplier == null) {
87+
resizeSupplier = imageResizeService;
88+
}
7989
FileInfo fileInfo = file.getFileInfo();
8090
ThumbnailObject thumbnailObject = new ThumbnailObject(THUMBNAIL_OBJECT_TYPE, Long.toString(fileInfo.getId()));
81-
List<MetadataItem> metadataItemList = metadataService.getMetadataItemsByMetadataTypeAndObject(THUMBNAIL_METADATA_TYPE.getName(),
91+
List<MetadataItem> metadataItemList =
92+
metadataService.getMetadataItemsByMetadataTypeAndObject(THUMBNAIL_METADATA_TYPE.getName(),
8293
thumbnailObject);
8394
List<MetadataItem> items = metadataItemList.stream()
8495
.filter(metadataItem -> metadataItem.getProperties() != null
85-
&& metadataItem.getProperties()
86-
.get(THUMBNAIL_WIDTH_PROPERTY)
87-
.equals(String.valueOf(width))
88-
&& metadataItem.getProperties()
89-
.get(THUMBNAIL_HEIGHT_PROPERTY)
90-
.equals(String.valueOf(height)))
96+
&& metadataItem.getProperties()
97+
.get(THUMBNAIL_WIDTH_PROPERTY)
98+
.equals(String.valueOf(width))
99+
&& metadataItem.getProperties()
100+
.get(THUMBNAIL_HEIGHT_PROPERTY)
101+
.equals(String.valueOf(height)))
91102
.toList();
92103
if (!items.isEmpty()) {
93104
long fileId = Long.parseLong(items.get(0).getParentObjectId());
94105
try {
95106
return fileService.getFile(fileId);
96107
} catch (FileStorageException e) {
97-
LOG.warn("Error while getting thumbnail for image of identity {}, original Image will be returned", identity.getId(), e.getMessage());
108+
LOG.warn("Error while getting thumbnail for image with file Id {}, original Image will be returned",
109+
fileId,
110+
e.getMessage());
98111
return file;
99112
}
100113
} else {
101-
byte[] imageContent = imageResizeService.scaleImage(IOUtils.toByteArray(file.getAsStream()), width, height, false, false);
114+
byte[] imageContent = resizeSupplier.scaleImage(IOUtils.toByteArray(file.getAsStream()), width, height, false, false);
102115
FileItem thumbnail = new FileItem(null,
103116
fileInfo.getName(),
104117
fileInfo.getMimetype(),
@@ -117,7 +130,7 @@ public FileItem getOrCreateThumbnail(FileItem file, Identity identity, int width
117130
Map<String, String> properties = new HashMap<>();
118131
properties.put(THUMBNAIL_WIDTH_PROPERTY, String.valueOf(width));
119132
properties.put(THUMBNAIL_HEIGHT_PROPERTY, String.valueOf(height));
120-
metadataService.createMetadataItem(thumbnailMetadataObject, metadataKey, properties, Long.parseLong(identity.getId()));
133+
metadataService.createMetadataItem(thumbnailMetadataObject, metadataKey, properties);
121134
return thumbnailFileItem;
122135
}
123136
}

component/core/src/test/java/org/exoplatform/social/attachment/AttachmentServiceTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,26 +298,24 @@ public void testGetAttachment() throws Exception { // NOSONAR
298298

299299
public void testGetAttachmentInputStream() throws Exception { // NOSONAR
300300
String fileId = createAttachment(USERNAME);
301-
Identity userAcl = startSessionAndRegisterAs(USERNAME);
301+
Identity userAclIdentity = startSessionAndRegisterAs(USERNAME);
302302

303+
assertThrows(IllegalAccessException.class,
304+
() -> attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, fileId, "0x0", userAclIdentity));
305+
hasViewPermission.set(true);
303306
assertThrows(IllegalArgumentException.class,
304-
() -> attachmentService.getAttachmentInputStream(null, objectId, fileId, "0x0", userAcl));
305-
assertThrows(IllegalArgumentException.class,
306-
() -> attachmentService.getAttachmentInputStream(OBJECT_TYPE, null, fileId, "0x0", userAcl));
307+
() -> attachmentService.getAttachmentInputStream(null, objectId, fileId, "0x0", userAclIdentity));
307308
assertThrows(IllegalArgumentException.class,
308-
() -> attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, fileId, "0x0", null));
309+
() -> attachmentService.getAttachmentInputStream(OBJECT_TYPE, null, fileId, "0x0", userAclIdentity));
309310
assertThrows(IllegalArgumentException.class,
310-
() -> attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, null, "0x0", userAcl));
311-
hasEditPermission.set(true);
312-
assertThrows(IllegalAccessException.class,
313-
() -> attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, fileId, null, userAcl));
311+
() -> attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, null, "0x0", userAclIdentity));
314312
hasEditPermission.set(false);
315313
hasViewPermission.set(true);
316314

317-
try (InputStream inputStream = attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, fileId, "0x0", userAcl)) {
315+
try (InputStream inputStream = attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, fileId, "0x0", userAclIdentity)) {
318316
assertNotNull(inputStream);
319317
}
320-
try (InputStream inputStream = attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, fileId, null, userAcl)) {
318+
try (InputStream inputStream = attachmentService.getAttachmentInputStream(OBJECT_TYPE, objectId, fileId, null, userAclIdentity)) {
321319
assertNotNull(inputStream);
322320
}
323321
}

0 commit comments

Comments
 (0)