-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat/#28-image-storage] 이미지 삭제 및 저장
- Loading branch information
Showing
8 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
### data ### | ||
docker/redis | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/coastee/server/image/domain/DirName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.coastee.server.image.domain; | ||
|
||
import com.coastee.server.auth.domain.Authority; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum DirName { | ||
USER("user"), POST("post"), CHAT("chat"); | ||
|
||
private final String code; | ||
|
||
@Override | ||
public String toString() { | ||
return this.code; | ||
} | ||
|
||
public static Authority of(final String code) { | ||
return Arrays.stream(Authority.values()) | ||
.filter(r -> r.getCode().equals(code.toUpperCase())) | ||
.findAny() | ||
.orElse(null); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/coastee/server/image/service/BlobStorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.coastee.server.image.service; | ||
|
||
import com.azure.core.util.BinaryData; | ||
import com.azure.core.util.Context; | ||
import com.azure.storage.blob.BlobClient; | ||
import com.azure.storage.blob.BlobContainerClient; | ||
import com.azure.storage.blob.models.BlobHttpHeaders; | ||
import com.azure.storage.blob.models.BlobRequestConditions; | ||
import com.azure.storage.blob.options.BlobParallelUploadOptions; | ||
import com.coastee.server.global.apipayload.exception.GeneralException; | ||
import com.coastee.server.image.domain.DirName; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.coastee.server.global.apipayload.code.status.ErrorStatus.IO_EXCEPTION; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BlobStorageService { | ||
private final BlobContainerClient blobContainerClient; | ||
|
||
@Value("${spring.cloud.azure.storage.blob.account-name}") | ||
private String account; | ||
|
||
@Value("${spring.cloud.azure.storage.blob.container-name}") | ||
private String container; | ||
|
||
public String upload( | ||
final MultipartFile file, | ||
final DirName imageDir, | ||
final Long id | ||
) { | ||
String fileName = imageDir + "/" + id; | ||
|
||
BlobClient blobClient = blobContainerClient.getBlobClient(fileName); | ||
BlobParallelUploadOptions uploadOptions = getUploadOptions(file); | ||
blobClient.uploadWithResponse(uploadOptions, null, Context.NONE); | ||
|
||
return String.format("https://%s.blob.core.windows.net/%s/%s/%s", account, container, imageDir, id); | ||
} | ||
|
||
public void delete( | ||
final DirName imageDir, | ||
final Long id | ||
) { | ||
String fileName = imageDir + "/" + id; | ||
BlobClient blobClient = blobContainerClient.getBlobClient(fileName); | ||
blobClient.delete(); | ||
} | ||
|
||
private BlobParallelUploadOptions getUploadOptions(final MultipartFile file) { | ||
try { | ||
BlobHttpHeaders jsonHeaders = new BlobHttpHeaders() | ||
.setContentType(file.getContentType()); | ||
BinaryData data = BinaryData.fromStream(file.getInputStream(), file.getSize()); | ||
return new BlobParallelUploadOptions(data) | ||
.setRequestConditions(new BlobRequestConditions()) | ||
.setHeaders(jsonHeaders); | ||
} catch (IOException e) { | ||
throw new GeneralException(IO_EXCEPTION); | ||
} | ||
} | ||
} |
22 changes: 20 additions & 2 deletions
22
src/main/java/com/coastee/server/user/domain/Experience.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters