Skip to content

Commit

Permalink
Always send clients unique image names
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Sep 21, 2024
1 parent 80d287f commit 93d4d99
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
30 changes: 14 additions & 16 deletions src/main/java/app/fyreplace/api/data/StoredFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.PostPersist;
import jakarta.persistence.PreRemove;
import jakarta.persistence.PostRemove;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.UUID;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

@Entity
Expand All @@ -42,9 +43,12 @@ public StoredFile() {
initServices();
}

public StoredFile(final String directory, final String name, @Nullable final byte[] data) {
public StoredFile(final String directory, @Nullable final byte[] data) {
initServices();
this.path = Paths.get(directory, name) + "." + mimeTypeService.getExtension(data);
this.path = UriBuilder.fromPath(directory)
.path(UUID.randomUUID() + "." + mimeTypeService.getExtension(data))
.build()
.getPath();
this.data = data;
}

Expand All @@ -58,24 +62,18 @@ public String toString() {
}
}

public void store(final byte[] data) throws IOException {
if (data != null) {
storageService.store(path, data);
}
}

@SuppressWarnings("unused")
@PostPersist
final void postPersist() throws IOException {
@PrePersist
final void prePersist() throws IOException {
if (data != null) {
store(data);
storageService.store(path, data);
data = null;
}
}

@SuppressWarnings("unused")
@PreRemove
final void preDestroy() throws IOException {
@PostRemove
final void postRemove() throws IOException {
storageService.remove(path);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/app/fyreplace/api/data/dev/DataSeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public User createUser(

if (hasAvatar) {
try (final var stream = getClass().getResourceAsStream("/META-INF/resources/images/logo-maskable.png")) {
final var storedFile = new StoredFile(
"avatars", user.username, requireNonNull(stream).readAllBytes());
final var storedFile =
new StoredFile("avatars", requireNonNull(stream).readAllBytes());
storedFile.persist();
user.avatar = storedFile;
user.persist();
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/app/fyreplace/api/endpoints/ChaptersEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,17 @@ public String setChapterImage(
try {
final var chapter = getChapter(post, position);
final var image = ImageIO.read(new ByteArrayInputStream(input));
final var oldImage = chapter.image;
chapter.width = image.getWidth();
chapter.height = image.getHeight();
chapter.image = new StoredFile("chapters", input);
chapter.image.persist();
chapter.persist();

if (chapter.image == null) {
chapter.image = new StoredFile("chapters", chapter.id.toString(), input);
} else {
chapter.image.store(input);
if (oldImage != null) {
oldImage.delete();
}

chapter.persist();
return chapter.image.toString();
} catch (final IndexOutOfBoundsException e) {
throw new NotFoundException();
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/app/fyreplace/api/endpoints/UsersEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.SecurityContext;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
Expand Down Expand Up @@ -244,17 +243,18 @@ public String setCurrentUserBio(@NotNull @Length(max = User.BIO_MAX_LENGTH) fina
@APIResponse(responseCode = "200", description = "OK")
@APIResponse(responseCode = "413", description = "Payload Too Large")
@APIResponse(responseCode = "415", description = "Unsupported Media Type")
public String setCurrentUserAvatar(final byte[] input) throws IOException {
public String setCurrentUserAvatar(final byte[] input) {
mimeTypeService.validate(input);
final var user = User.getFromSecurityContext(context, LockModeType.PESSIMISTIC_WRITE);
final var oldAvatar = user.avatar;
user.avatar = new StoredFile("avatars", input);
user.avatar.persist();
user.persist();

if (user.avatar == null) {
user.avatar = new StoredFile("avatars", user.username, input);
} else {
user.avatar.store(input);
if (oldAvatar != null) {
oldAvatar.delete();
}

user.persist();
return user.avatar.toString();
}

Expand Down

0 comments on commit 93d4d99

Please sign in to comment.