Skip to content

Commit

Permalink
add cloudinary image upload + env
Browse files Browse the repository at this point in the history
  • Loading branch information
dinaraparanid committed Jan 2, 2025
1 parent 415c7ed commit 9b8134e
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ pubspec.lock
firebase.json
lib/firebase_options.dart
firepit-log.txt

.env
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@ SPEC CHECKSUMS:
FirebaseMessaging: f8a160d99c2c2e5babbbcc90c4a3e15db036aee2
FirebaseSharedSwift: a4e5dfca3e210633bb3a3dfb94176c019211948b
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_native_splash: f71420956eb811e6d310720fee915f1d42852e7a
flutter_native_splash: e8a1e01082d97a8099d973f919f57904c925008a
GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7
GoogleUtilities: 26a3abef001b6533cf678d3eb38fd3f614b7872d
"gRPC-C++": 2fa52b3141e7789a28a737f251e0c45b4cb20a87
Expand Down
5 changes: 5 additions & 0 deletions lib/core/data/cloudinary/cloudinary_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:cloudinary/cloudinary.dart';
import 'package:poster/core/data/env/env.dart';

Cloudinary createCloudinaryClient() =>
Cloudinary.unsignedConfig(cloudName: Env.cloudinaryCloudName);
9 changes: 9 additions & 0 deletions lib/core/data/cloudinary/di/cloudinary_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:cloudinary/cloudinary.dart';
import 'package:get_it/get_it.dart';
import 'package:poster/core/data/cloudinary/cloudinary_client.dart';
import 'package:poster/core/di/provide_singleton.dart';

extension CloudinaryModule on GetIt {
List<Type> registerCloudinaryModule() =>
[provideSingleton<Cloudinary>(() => createCloudinaryClient())];
}
12 changes: 12 additions & 0 deletions lib/core/data/env/env.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:envied/envied.dart';

part 'env.g.dart';

@envied
final class Env {
@EnviedField(varName: 'CLOUDINARY_CLOUD_NAME', obfuscate: true, defaultValue: 'placeholder')
static final String cloudinaryCloudName = _Env.cloudinaryCloudName;

@EnviedField(varName: 'CLOUDINARY_UPLOAD_PRESET', obfuscate: true, defaultValue: 'placeholder')
static final String cloudinaryUploadPreset = _Env.cloudinaryUploadPreset;
}
10 changes: 10 additions & 0 deletions lib/core/data/image/di/image_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:get_it/get_it.dart';
import 'package:poster/core/data/image/image_repository_impl.dart';
import 'package:poster/core/di/provide_singleton.dart';
import 'package:poster/core/domain/image/image_repository.dart';

extension ImageModule on GetIt {
List<Type> registerImageModule() => [
provideSingleton<ImageRepository>(() => ImageRepositoryImpl(client: this())),
];
}
31 changes: 31 additions & 0 deletions lib/core/data/image/image_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:cloudinary/cloudinary.dart';
import 'package:fpdart/fpdart.dart';
import 'package:image_picker/image_picker.dart';
import 'package:poster/core/data/env/env.dart';
import 'package:poster/core/domain/image/image_repository.dart';

final class ImageRepositoryImpl extends ImageRepository {
final Cloudinary _client;
ImageRepositoryImpl({required Cloudinary client}) : _client = client;

@override
Future<Either<Exception, String>> upload(XFile file) async {
final response = await _client.unsignedUpload(
file: file.path,
uploadPreset: Env.cloudinaryUploadPreset,
fileBytes: await file.readAsBytes(),
fileName: file.name,
resourceType: CloudinaryResourceType.image,
);

final url = response.secureUrl;

if (response.isSuccessful && url != null) {
return Either.right(url);
}

return Either.left(Exception(
'Error uploading image: ${response.statusCode} - ${response.error}'
));
}
}
8 changes: 7 additions & 1 deletion lib/core/di/core_module.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import 'package:get_it/get_it.dart';
import 'package:poster/core/data/cloudinary/di/cloudinary_module.dart';
import 'package:poster/core/data/dio/di/dio_module.dart';
import 'package:poster/core/data/image/di/image_module.dart';
import 'package:poster/core/data/post/di/post_module.dart';
import 'package:poster/core/data/profile/di/profile_module.dart';

extension CoreModule on GetIt {
List<Type> registerCoreModule() {
registerPostModule();
registerDio();
return [...registerProfileModule()];
return [
...registerProfileModule(),
...registerCloudinaryModule(),
...registerImageModule(),
];
}
}
6 changes: 6 additions & 0 deletions lib/core/domain/image/image_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:fpdart/fpdart.dart';
import 'package:image_picker/image_picker.dart';

abstract class ImageRepository {
Future<Either<Exception, String>> upload(XFile file);
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import FlutterMacOS
import Foundation

import cloud_firestore
import file_selector_macos
import firebase_auth
import firebase_core
import firebase_messaging
import shared_preferences_foundation

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAuthPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin"))
Expand Down
6 changes: 6 additions & 0 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,8 @@ PODS:
- Firebase/Firestore (~> 11.4.0)
- firebase_core
- FlutterMacOS
- file_selector_macos (0.0.1):
- FlutterMacOS
- Firebase/Auth (11.4.2):
- Firebase/CoreOnly
- FirebaseAuth (~> 11.4.0)
Expand Down Expand Up @@ -1366,6 +1368,7 @@ PODS:

DEPENDENCIES:
- cloud_firestore (from `Flutter/ephemeral/.symlinks/plugins/cloud_firestore/macos`)
- file_selector_macos (from `Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos`)
- firebase_auth (from `Flutter/ephemeral/.symlinks/plugins/firebase_auth/macos`)
- firebase_core (from `Flutter/ephemeral/.symlinks/plugins/firebase_core/macos`)
- firebase_messaging (from `Flutter/ephemeral/.symlinks/plugins/firebase_messaging/macos`)
Expand Down Expand Up @@ -1400,6 +1403,8 @@ SPEC REPOS:
EXTERNAL SOURCES:
cloud_firestore:
:path: Flutter/ephemeral/.symlinks/plugins/cloud_firestore/macos
file_selector_macos:
:path: Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos
firebase_auth:
:path: Flutter/ephemeral/.symlinks/plugins/firebase_auth/macos
firebase_core:
Expand All @@ -1415,6 +1420,7 @@ SPEC CHECKSUMS:
abseil: d121da9ef7e2ff4cab7666e76c5a3e0915ae08c3
BoringSSL-GRPC: ca6a8e5d04812fce8ffd6437810c2d46f925eaeb
cloud_firestore: 0d1c982890702bf99ab2881743c35fddb66e295d
file_selector_macos: cc3858c981fe6889f364731200d6232dac1d812d
Firebase: 7fd5466678d964be78fbf536d8a3385da19c4828
firebase_auth: cda334a14540e3c1be90eb2cd82682f9409bea37
firebase_core: 1dfe1f4d02ad78be0277e320aa3d8384cf46231f
Expand Down
2 changes: 2 additions & 0 deletions macos/Runner/DebugProfile.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.paranid5.poster</string>
Expand Down
2 changes: 2 additions & 0 deletions macos/Runner/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.paranid5.poster</string>
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies:
flutter_svg: ^2.0.16
go_router: ^14.6.2
flutter_native_splash: ^2.4.3
cloudinary: ^1.2.0
envied: ^1.0.0
image_picker: ^1.1.2

dev_dependencies:
flutter_test:
Expand All @@ -41,6 +44,7 @@ dev_dependencies:
json_serializable: ^6.8.0
flutter_launcher_icons: "^0.14.2"
test: ^1.25.8
envied_generator: ^1.0.0

flutter:
uses-material-design: true
Expand Down

0 comments on commit 9b8134e

Please sign in to comment.