Fixes Storage API Endpoints and Adds ProfilePicture Deletion

This commit is contained in:
2023-09-10 02:41:21 -03:00
parent dc4027b954
commit 066443d430
2 changed files with 56 additions and 21 deletions

View File

@@ -6,7 +6,6 @@ import com.hideyoshi.backendportfolio.base.user.model.TokenDTO;
import com.hideyoshi.backendportfolio.base.user.model.UserDTO; import com.hideyoshi.backendportfolio.base.user.model.UserDTO;
import com.hideyoshi.backendportfolio.base.user.service.UserService; import com.hideyoshi.backendportfolio.base.user.service.UserService;
import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum; import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum;
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceDownloadResponse;
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse; import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse;
import com.hideyoshi.backendportfolio.microservice.storageService.service.StorageService; import com.hideyoshi.backendportfolio.microservice.storageService.service.StorageService;
import com.hideyoshi.backendportfolio.util.guard.UserResourceGuard; import com.hideyoshi.backendportfolio.util.guard.UserResourceGuard;
@@ -68,14 +67,21 @@ public class UserController {
@UserResourceGuard(accessType = UserResourceGuardEnum.USER) @UserResourceGuard(accessType = UserResourceGuardEnum.USER)
public ResponseEntity<Void> deleteMyUser() { public ResponseEntity<Void> deleteMyUser() {
UserDTO loggedUser = this.authService.getLoggedUser(); UserDTO loggedUser = this.authService.getLoggedUser();
this.userService.deleteUser(loggedUser.getId()); this.userService.deleteUser(loggedUser.getId());
this.storageService.deleteFile(loggedUser.getUsername(), "profile");
return new ResponseEntity<>(HttpStatus.NO_CONTENT); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} }
@DeleteMapping("/delete/{id}") @DeleteMapping("/delete/{id}")
@UserResourceGuard(accessType = UserResourceGuardEnum.ADMIN_USER) @UserResourceGuard(accessType = UserResourceGuardEnum.ADMIN_USER)
public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id) { public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id) {
this.userService.deleteUser(id); UserDTO user = this.userService.getUser(id);
this.userService.deleteUser(user.getId());
this.storageService.deleteFile(user.getUsername(), "profile");
return new ResponseEntity<>(HttpStatus.NO_CONTENT); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} }
@@ -92,6 +98,16 @@ public class UserController {
); );
} }
@DeleteMapping("/profile-picture")
@UserResourceGuard(accessType = UserResourceGuardEnum.USER)
public void deleteProfilePicture() {
UserDTO user = this.authService.getLoggedUser();
this.storageService.deleteFile(
user.getUsername(),
"profile"
);
}
@PostMapping("/profile-picture/proccess") @PostMapping("/profile-picture/proccess")
@UserResourceGuard(accessType = UserResourceGuardEnum.USER) @UserResourceGuard(accessType = UserResourceGuardEnum.USER)
public void processProfilePicture() { public void processProfilePicture() {

View File

@@ -8,19 +8,14 @@ import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageS
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse; import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.http.NameValuePair; import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -28,12 +23,7 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
@Log4j2 @Log4j2
@Service @Service
@@ -67,7 +57,7 @@ public class StorageService {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
HttpPost request = new HttpPost(URI.create(storageServiceConfig.getFileServicePath() + "/new_file_url")); HttpPost request = new HttpPost(URI.create(storageServiceConfig.getFileServicePath() + "/file"));
request.setHeader("Content-Type", "application/json"); request.setHeader("Content-Type", "application/json");
try { try {
@@ -95,7 +85,7 @@ public class StorageService {
public StorageServiceDownloadResponse getFileUrl(String username, String filePostfix) { public StorageServiceDownloadResponse getFileUrl(String username, String filePostfix) {
URI uri = null; URI uri = null;
try { try {
uri = new URIBuilder(storageServiceConfig.getFileServicePath() + "/file_url") uri = new URIBuilder(storageServiceConfig.getFileServicePath() + "/file")
.addParameter(PARAMETER_USERNAME, username) .addParameter(PARAMETER_USERNAME, username)
.addParameter(PARAMETER_FILE_POSTFIX, filePostfix) .addParameter(PARAMETER_FILE_POSTFIX, filePostfix)
.build(); .build();
@@ -122,6 +112,35 @@ public class StorageService {
} }
} }
public void deleteFile(String username, String filePostfix) {
URI uri = null;
try {
uri = new URIBuilder(storageServiceConfig.getFileServicePath() + "/file")
.addParameter(PARAMETER_USERNAME, username)
.addParameter(PARAMETER_FILE_POSTFIX, filePostfix)
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
HttpDelete request = new HttpDelete(uri);
request.setHeader("Content-Type", "application/json");
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy()).build();
try {
httpClient.execute(
request,
response -> {
return null;
}
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void processFile(String username, String filePostfix) { public void processFile(String username, String filePostfix) {
HashMap<String, String> values = new HashMap<>() {{ HashMap<String, String> values = new HashMap<>() {{
put(PARAMETER_USERNAME, username); put(PARAMETER_USERNAME, username);
@@ -138,7 +157,7 @@ public class StorageService {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
HttpPost request = new HttpPost(URI.create(storageServiceConfig.getFileServicePath() + "/process_file")); HttpPost request = new HttpPost(URI.create(storageServiceConfig.getFileServicePath() + "/file/process"));
request.setHeader("Content-Type", "application/json"); request.setHeader("Content-Type", "application/json");
try { try {