-
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.
- Loading branch information
Showing
2 changed files
with
65 additions
and
77 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,53 +1,79 @@ | ||
import 'dart:developer'; | ||
import 'dart:io'; | ||
|
||
import 'package:cogo/constants/apis.dart'; | ||
import 'package:cogo/data/di/api_client.dart'; | ||
import 'package:cogo/data/dto/response/base_response.dart'; | ||
import 'package:cogo/data/dto/response/image_save_response.dart'; | ||
import 'package:cogo/data/repository/local/secure_storage_repository.dart'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter_config/flutter_config.dart'; | ||
|
||
import '../../../constants/apis.dart'; | ||
|
||
class S3Service { | ||
final ApiClient _apiClient = ApiClient(); | ||
static const apiVersion = "api/v2/"; | ||
final Dio _dio = Dio(); | ||
final SecureStorageRepository _secureStorage = SecureStorageRepository(); | ||
|
||
S3Service(); | ||
|
||
// s3 이미지 저장 | ||
Future<ImageSaveResponse> saveImage(File selectedImage) async { | ||
Future<String?> uploadImage(String imagePath) async { | ||
try { | ||
String fileName = selectedImage.path.split('/').last; | ||
log("fileName: $fileName"); | ||
|
||
FormData formData = FormData.fromMap({ | ||
"file": await MultipartFile.fromFile( | ||
selectedImage.path, | ||
filename: fileName, | ||
), | ||
// API 엔드포인트 URL | ||
const apiVersion = "api/v2/"; | ||
final url = '${FlutterConfig.get("base_url")}$apiVersion${Apis.s3}/v2'; | ||
|
||
// 토큰 가져오기 | ||
var token = await _secureStorage.readAccessToken(); | ||
|
||
// FormData 생성 | ||
final formData = FormData.fromMap({ | ||
'image': await MultipartFile.fromFile(imagePath, | ||
filename: imagePath.split('/').last) | ||
}); | ||
|
||
log("formData: ${formData.files.length}"); | ||
// Dio 요청 옵션 설정 | ||
final options = Options( | ||
headers: { | ||
'accept': '*/*', | ||
'Authorization': 'Bearer $token', | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
); | ||
|
||
// 요청 전송 | ||
final response = await _dio.post( | ||
url, | ||
data: formData, | ||
options: options, | ||
); | ||
|
||
final response = await _apiClient.dio.post( | ||
options: Options( | ||
extra: {'skipAuthToken': false, 'multipart': true}, //토큰 해제하지 않음 | ||
), | ||
'${Apis.s3}/v2', | ||
data: {'image': formData.files}); | ||
// Log Interceptor 추가 | ||
_dio.interceptors.add(LogInterceptor( | ||
request: true, | ||
requestHeader: true, | ||
requestBody: true, | ||
responseHeader: true, | ||
responseBody: true, | ||
error: true, | ||
logPrint: (obj) { | ||
log("서버통신 $obj"); | ||
}, | ||
)); | ||
|
||
if (response.statusCode == 201) { | ||
//base response로 받는건 여기서 뿐임. | ||
final baseResponse = BaseResponse<ImageSaveResponse>.fromJson( | ||
response.data, | ||
(contentJson) => ImageSaveResponse.fromJson(contentJson), | ||
); | ||
return baseResponse.content; | ||
return baseResponse.content.savedUrl; | ||
} else { | ||
throw Exception('Failed to send verification code'); | ||
throw Exception('서버 통신 실패 ${response.statusCode}'); | ||
} | ||
} on DioException catch (e) { | ||
throw Exception('Error: ${e.response?.data ?? e.message}'); | ||
// Dio 특화된 에러 처리 | ||
throw Exception('업로드 중 오류 발생: ${e.response?.data ?? e.message}'); | ||
} catch (e) { | ||
// 기타 모든 예외 처리 | ||
throw Exception('An unexpected error occurred: $e'); | ||
// 기타 예외 처리 | ||
throw Exception('업로드 중 오류 발생: ${e.toString()}'); | ||
} | ||
} | ||
} | ||
} |
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