feat: added photo service

This commit is contained in:
Petr Rudichev 2025-02-19 17:37:59 +03:00
parent 98d238f9c9
commit 4b721518e0
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package com.example.nto.service;
public interface PhotoService {
String uploadProfilePhoto(long id, byte[] photo);
}

View File

@ -0,0 +1,45 @@
package com.example.nto.service.impl;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.example.nto.aspect.annotation.LogExample;
import com.example.nto.config.ObjectStorageConfig;
import com.example.nto.service.PhotoService;
import com.example.nto.utils.Utils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
@Service
@RequiredArgsConstructor
public class PhotoServiceImpl implements PhotoService {
private final ObjectStorageConfig objectStorageConfig;
@Override
@LogExample
public String uploadProfilePhoto(long id, byte[] photo) {
final String url;
final String bucketName = objectStorageConfig.getBucketName();
final AmazonS3 s3Client = objectStorageConfig.createAmazonS3();
try {
String fileName = Utils.profileFileName(id);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(photo.length);
// Загрузка файла в Yandex Object Storage
ByteArrayInputStream inputStream = new ByteArrayInputStream(photo);
s3Client.putObject(bucketName, fileName, inputStream, metadata);
// Получение ссылки на загруженный файл
url = s3Client.getUrl(bucketName, fileName).toExternalForm();
} catch (AmazonS3Exception e) {
throw new AmazonS3Exception(e.getMessage());
}
return url;
}
}