Skip to content

Commit 001d92a

Browse files
authored
use String Constant to replace hardcoded "s3" string literals to avoid migration not changing the string value stably (#69)
* move generate url to interface * replace s3 string constant to storage
1 parent c7ec910 commit 001d92a

File tree

11 files changed

+52
-34
lines changed

11 files changed

+52
-34
lines changed

asset-manager/web/src/main/java/com/microsoft/migration/assets/config/WebMvcConfig.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.migration.assets.config;
22

3+
import com.microsoft.migration.assets.constants.StorageConstants;
34
import org.springframework.context.annotation.Configuration;
45
import org.springframework.http.CacheControl;
56
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -37,8 +38,8 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
3738
@Override
3839
public void addInterceptors(InterceptorRegistry registry) {
3940
registry.addInterceptor(new FileOperationLoggingInterceptor())
40-
.addPathPatterns("/s3/**")
41-
.excludePathPatterns("/s3/view/**"); // Exclude file download endpoints from detailed logging
41+
.addPathPatterns("/" + StorageConstants.STORAGE_PATH + "/**")
42+
.excludePathPatterns("/" + StorageConstants.STORAGE_PATH + "/view/**"); // Exclude file download endpoints from detailed logging
4243
}
4344

4445
/**
@@ -89,7 +90,7 @@ private String determineFileOperation(HttpServletRequest request) {
8990
return "FILE_DOWNLOAD";
9091
} else if (uri.contains("/view-page/")) {
9192
return "FILE_VIEW_PAGE";
92-
} else if ("GET".equals(method) && uri.equals("/s3")) {
93+
} else if ("GET".equals(method) && uri.equals("/" + StorageConstants.STORAGE_PATH)) {
9394
return "FILE_LIST";
9495
} else {
9596
return "FILE_OPERATION";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.microsoft.migration.assets.constants;
2+
3+
/**
4+
* Application constants for storage paths and configurations
5+
*/
6+
public final class StorageConstants {
7+
8+
/**
9+
* Base storage path for web endpoints
10+
*/
11+
public static final String STORAGE_PATH = "storage";
12+
13+
private StorageConstants() {
14+
// Utility class - prevent instantiation
15+
}
16+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.migration.assets.controller;
22

3+
import com.microsoft.migration.assets.constants.StorageConstants;
34
import org.springframework.stereotype.Controller;
45
import org.springframework.web.bind.annotation.GetMapping;
56

@@ -8,6 +9,6 @@ public class HomeController {
89

910
@GetMapping("/")
1011
public String home() {
11-
return "redirect:/s3";
12+
return "redirect:/" + StorageConstants.STORAGE_PATH;
1213
}
1314
}

asset-manager/web/src/main/java/com/microsoft/migration/assets/controller/S3Controller.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.migration.assets.controller;
22

3+
import com.microsoft.migration.assets.constants.StorageConstants;
34
import com.microsoft.migration.assets.model.S3StorageItem;
45
import com.microsoft.migration.assets.service.StorageService;
56
import lombok.RequiredArgsConstructor;
@@ -19,7 +20,7 @@
1920
import java.util.Optional;
2021

2122
@Controller
22-
@RequestMapping("/s3")
23+
@RequestMapping("/" + StorageConstants.STORAGE_PATH)
2324
@RequiredArgsConstructor
2425
public class S3Controller {
2526

@@ -42,15 +43,15 @@ public String uploadObject(@RequestParam("file") MultipartFile file, RedirectAtt
4243
try {
4344
if (file.isEmpty()) {
4445
redirectAttributes.addFlashAttribute("error", "Please select a file to upload");
45-
return "redirect:/s3/upload";
46+
return "redirect:/" + StorageConstants.STORAGE_PATH + "/upload";
4647
}
4748

4849
storageService.uploadObject(file);
4950
redirectAttributes.addFlashAttribute("success", "File uploaded successfully");
50-
return "redirect:/s3";
51+
return "redirect:/" + StorageConstants.STORAGE_PATH;
5152
} catch (IOException e) {
5253
redirectAttributes.addFlashAttribute("error", "Failed to upload file: " + e.getMessage());
53-
return "redirect:/s3/upload";
54+
return "redirect:/" + StorageConstants.STORAGE_PATH + "/upload";
5455
}
5556
}
5657

@@ -67,11 +68,11 @@ public String viewObjectPage(@PathVariable String key, Model model, RedirectAttr
6768
return "view";
6869
} else {
6970
redirectAttributes.addFlashAttribute("error", "Image not found");
70-
return "redirect:/s3";
71+
return "redirect:/" + StorageConstants.STORAGE_PATH;
7172
}
7273
} catch (Exception e) {
7374
redirectAttributes.addFlashAttribute("error", "Failed to view image: " + e.getMessage());
74-
return "redirect:/s3";
75+
return "redirect:/" + StorageConstants.STORAGE_PATH;
7576
}
7677
}
7778

@@ -100,6 +101,6 @@ public String deleteObject(@PathVariable String key, RedirectAttributes redirect
100101
} catch (Exception e) {
101102
redirectAttributes.addFlashAttribute("error", "Failed to delete file: " + e.getMessage());
102103
}
103-
return "redirect:/s3";
104+
return "redirect:/" + StorageConstants.STORAGE_PATH;
104105
}
105106
}

asset-manager/web/src/main/java/com/microsoft/migration/assets/service/AwsS3Service.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,6 @@ private String extractFilename(String key) {
145145
return lastSlashIndex >= 0 ? key.substring(lastSlashIndex + 1) : key;
146146
}
147147

148-
private String generateUrl(String key) {
149-
// Use application proxy URL for consistent behavior across storage types
150-
return "/s3/view/" + key;
151-
}
152-
153148
private String generateKey(String filename) {
154149
return UUID.randomUUID().toString() + "-" + filename;
155150
}

asset-manager/web/src/main/java/com/microsoft/migration/assets/service/LocalFileStorageService.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,4 @@ public void deleteObject(String key) throws IOException {
141141
public String getStorageType() {
142142
return "local";
143143
}
144-
145-
private String generateUrl(String key) {
146-
// Generate a URL for the object (simplified path)
147-
return "/s3/view/" + key;
148-
}
149144
}

asset-manager/web/src/main/java/com/microsoft/migration/assets/service/StorageService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.microsoft.migration.assets.service;
22

3+
import com.microsoft.migration.assets.constants.StorageConstants;
4+
import com.microsoft.migration.assets.model.ImageProcessingMessage;
35
import com.microsoft.migration.assets.model.S3StorageItem;
46
import org.springframework.web.multipart.MultipartFile;
57

@@ -48,4 +50,11 @@ default String getThumbnailKey(String key) {
4850
}
4951
return key + "_thumbnail";
5052
}
53+
54+
/**
55+
* Generate a URL for viewing the object
56+
*/
57+
default String generateUrl(String key) {
58+
return "/" + StorageConstants.STORAGE_PATH + "/view/" + key;
59+
}
5160
}

asset-manager/web/src/main/resources/templates/layout.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
<div class="d-flex align-items-center text-dark text-decoration-none">
2424
<h1 class="fs-4">AWS S3 Asset Manager</h1>
2525
<div class="ms-auto">
26-
<a class="btn btn-outline-primary me-2" th:href="@{/s3}">All Images</a>
27-
<a class="btn btn-primary" th:href="@{/s3/upload}">Upload New Image</a>
26+
<a class="btn btn-outline-primary me-2" th:href="@{/storage}">All Images</a>
27+
<a class="btn btn-primary" th:href="@{/storage/upload}">Upload New Image</a>
2828
</div>
2929
</div>
3030
</header>

asset-manager/web/src/main/resources/templates/list.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h2>Your Images</h2>
77
<div class="row mt-4" id="imageContainer" th:if="${not #lists.isEmpty(objects)}">
88
<div class="col-md-4 mb-4" th:each="object : ${objects}" th:attr="data-key=${object.key}">
99
<div class="card">
10-
<img th:src="@{'/s3/view/' + ${object.key}}" class="card-img-top" alt="Image preview" style="height: 200px; object-fit: cover;">
10+
<img th:src="@{'/storage/view/' + ${object.key}}" class="card-img-top" alt="Image preview" style="height: 200px; object-fit: cover;">
1111
<div class="card-body">
1212
<h5 class="card-title text-truncate" th:text="${object.name}">Image name</h5>
1313
<p class="card-text">
@@ -17,8 +17,8 @@ <h5 class="card-title text-truncate" th:text="${object.name}">Image name</h5>
1717
</small>
1818
</p>
1919
<div class="d-flex justify-content-between">
20-
<a th:href="@{'/s3/view-page/' + ${object.key}}" class="btn btn-primary btn-sm">View</a>
21-
<form th:action="@{'/s3/delete/' + ${object.key}}" method="post" onsubmit="return confirm('Are you sure you want to delete this file?');">
20+
<a th:href="@{'/storage/view-page/' + ${object.key}}" class="btn btn-primary btn-sm">View</a>
21+
<form th:action="@{'/storage/delete/' + ${object.key}}" method="post" onsubmit="return confirm('Are you sure you want to delete this file?');">
2222
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
2323
</form>
2424
</div>
@@ -28,7 +28,7 @@ <h5 class="card-title text-truncate" th:text="${object.name}">Image name</h5>
2828
</div>
2929

3030
<div class="alert alert-info" th:if="${#lists.isEmpty(objects)}">
31-
No images found in the S3 bucket. <a th:href="@{/s3/upload}" class="alert-link">Upload your first image!</a>
31+
No images found in the S3 bucket. <a th:href="@{/storage/upload}" class="alert-link">Upload your first image!</a>
3232
</div>
3333

3434
<!-- Loading indicator for auto-refreshing -->

asset-manager/web/src/main/resources/templates/upload.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div th:fragment="content">
55
<h2>Upload Image to S3</h2>
66

7-
<form th:action="@{/s3/upload}" method="post" enctype="multipart/form-data" class="mt-4" id="uploadForm">
7+
<form th:action="@{/storage/upload}" method="post" enctype="multipart/form-data" class="mt-4" id="uploadForm">
88
<div class="mb-3">
99
<label for="file" class="form-label">Select Image</label>
1010
<input type="file" class="form-control" id="file" name="file" accept="image/*" required>
@@ -22,7 +22,7 @@ <h2>Upload Image to S3</h2>
2222

2323
<div class="mt-4">
2424
<button type="submit" class="btn btn-success me-2" id="uploadBtn">Upload</button>
25-
<a th:href="@{/s3}" class="btn btn-secondary">Cancel</a>
25+
<a th:href="@{/storage}" class="btn btn-secondary">Cancel</a>
2626
</div>
2727
</form>
2828

0 commit comments

Comments
 (0)