Skip to content

Commit

Permalink
General: Cache course icons and profile pictures to improve performan…
Browse files Browse the repository at this point in the history
…ce (#9459)
  • Loading branch information
krusche authored Oct 12, 2024
1 parent d801ef8 commit 330a677
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import jakarta.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.CacheControl;
Expand All @@ -32,6 +33,9 @@ public PublicResourcesConfiguration(JHipsterProperties jHipsterProperties) {
this.jHipsterProperties = jHipsterProperties;
}

@Value("${artemis.file-upload-path}")
private String fileUploadPath;

@Override
public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
// Enable static resource serving in general from "/public" from both classpath and hosts filesystem
Expand All @@ -46,6 +50,16 @@ public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {

addResourceHandlerForPath(registry, "images", "about").setCacheControl(defaultCacheControl);
addResourceHandlerForPath(registry, "emoji").setCacheControl(defaultCacheControl);

// Add caching for course icons, user profile pictures, and drag and drop quiz pictures
// Add resource handlers for dynamic image paths based on fileUploadPath
// TODO: those paths have to be the same as in FilePathService, ideally we reuse the constants and define them only once
registry.addResourceHandler("/images/course/icons/**").addResourceLocations("file:" + fileUploadPath + "/images/course/icons/").setCacheControl(defaultCacheControl);

registry.addResourceHandler("/images/user/profile-pictures/**").addResourceLocations("file:" + fileUploadPath + "/images/user/profile-pictures/")
.setCacheControl(defaultCacheControl);

registry.addResourceHandler("/images/drag-and-drop/**").addResourceLocations("file:" + fileUploadPath + "/images/drag-and-drop/").setCacheControl(defaultCacheControl);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -646,7 +647,8 @@ private ResponseEntity<byte[]> responseEntityForFilePath(Path filePath) {
if (file == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(file);
return ResponseEntity.ok().cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) // Cache for 30 days;
.contentType(getMediaTypeFromFilename(filePath.getFileName().toString())).body(file);
}
catch (IOException e) {
log.error("Failed to return requested file with path {}", filePath, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class ConversationMessagesComponent implements OnInit, AfterViewInit, OnD
this.createEmptyPost();
}
}

private subscribeToMetis() {
this.metisService.posts.pipe(takeUntil(this.ngUnsubscribe)).subscribe((posts: Post[]) => {
this.setPosts(posts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class MetisConversationService implements OnDestroy {

public setActiveConversation(conversationIdentifier: ConversationDTO | number | undefined) {
this.updateLastReadDateAndNumberOfUnreadMessages();
let cachedConversation = undefined;
let cachedConversation: ConversationDTO | undefined = undefined;
if (conversationIdentifier) {
const parameterJustId = typeof conversationIdentifier === 'number';
cachedConversation = this.conversationsOfUser.find(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3303,8 +3303,7 @@ private Course createCourseWithCourseImageAndReturn() throws Exception {
course = objectMapper.readValue(result.getResponse().getContentAsString(), Course.class);

assertThat(course.getCourseIcon()).as("Course icon got stored").isNotNull();
var imgResult = request.performMvcRequest(get(course.getCourseIcon())).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_OCTET_STREAM))
.andReturn();
var imgResult = request.performMvcRequest(get(course.getCourseIcon())).andExpect(status().isOk()).andExpect(content().contentType(MediaType.IMAGE_PNG)).andReturn();
assertThat(imgResult.getResponse().getContentAsByteArray()).isNotEmpty();

var createdCourse = courseRepo.findByIdElseThrow(course.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ private void checkCreatedFiles(QuizExercise quizExercise) throws Exception {
}

private void checkCreatedFile(String path) throws Exception {
MvcResult result = request.performMvcRequest(get(path)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_OCTET_STREAM)).andReturn();
MediaType mediaType = path.endsWith(".png") ? MediaType.IMAGE_PNG : MediaType.IMAGE_JPEG;
MvcResult result = request.performMvcRequest(get(path)).andExpect(status().isOk()).andExpect(content().contentType(mediaType)).andReturn();
byte[] image = result.getResponse().getContentAsByteArray();
assertThat(image).isNotEmpty();
}
Expand Down

0 comments on commit 330a677

Please sign in to comment.