Skip to content

Commit

Permalink
Add folder option to CloudinaryFile
Browse files Browse the repository at this point in the history
  • Loading branch information
djade007 committed Jul 8, 2021
1 parent 88b6b5d commit 992cb00
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:example/src/multi_image_picker_example.dart';
import 'package:example/src/image_picker_example.dart';
import 'package:flutter/material.dart';

void main() {
Expand All @@ -14,8 +14,8 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
// home: ImagePickerExample(),
home: MultiImagePickerExample(),
home: ImagePickerExample(),
// home: MultiImagePickerExample(),
);
}
}
1 change: 1 addition & 0 deletions example/lib/src/image_picker_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class _ImagePickerExampleState extends State<ImagePickerExample> {
final res = await cloudinary.uploadFile(
CloudinaryFile.fromFile(
_pickedFile.path,
folder: 'hello-folder',
),
);
print(res);
Expand Down
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -42,7 +42,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.8.1"
version: "0.8.1+1"
collection:
dependency: transitive
description:
Expand Down Expand Up @@ -92,7 +92,7 @@ packages:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.1"
version: "0.13.3"
http_parser:
dependency: transitive
description:
Expand Down Expand Up @@ -181,7 +181,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -216,7 +216,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
Expand Down
56 changes: 37 additions & 19 deletions lib/src/cloudinary_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class CloudinaryFile {
/// The file name/path
final String? identifier;

/// An optional folder name where the uploaded asset will be stored.
/// The public ID will contain the full path of the uploaded asset,
/// including the folder name.
final String? folder;

/// External url
final String? url;

Expand All @@ -31,14 +36,15 @@ class CloudinaryFile {
bool get fromExternalUrl => url != null;

/// [CloudinaryFile] instance
const CloudinaryFile(
{this.byteData,
this.filePath,
this.identifier,
this.url,
required this.resourceType,
this.tags})
: assert(
const CloudinaryFile({
this.resourceType: CloudinaryResourceType.Auto,
this.byteData,
this.filePath,
this.identifier,
this.url,
this.tags,
this.folder,
}) : assert(
(byteData == null && filePath != null) ||
(byteData != null && filePath == null) ||
url != null,
Expand All @@ -57,37 +63,49 @@ class CloudinaryFile {
);

/// Instantiate [CloudinaryFile] from [ByteData]
factory CloudinaryFile.fromByteData(ByteData byteData,
{String? identifier,
CloudinaryResourceType resourceType: CloudinaryResourceType.Auto,
List<String>? tags}) =>
factory CloudinaryFile.fromByteData(
ByteData byteData, {
String? identifier,
CloudinaryResourceType resourceType: CloudinaryResourceType.Auto,
List<String>? tags,
String? folder,
}) =>
CloudinaryFile(
byteData: byteData,
identifier: identifier,
resourceType: resourceType,
tags: tags,
folder: folder,
);

/// Instantiate [CloudinaryFile] from [File] path
factory CloudinaryFile.fromFile(String path,
{String? identifier,
CloudinaryResourceType resourceType: CloudinaryResourceType.Auto,
List<String>? tags}) =>
factory CloudinaryFile.fromFile(
String path, {
String? identifier,
CloudinaryResourceType resourceType: CloudinaryResourceType.Auto,
List<String>? tags,
String? folder,
}) =>
CloudinaryFile(
filePath: path,
identifier: identifier ??= path.split('/').last,
resourceType: resourceType,
tags: tags,
folder: folder,
);

/// Instantiate [CloudinaryFile] from an external url
factory CloudinaryFile.fromUrl(String url,
{CloudinaryResourceType resourceType: CloudinaryResourceType.Auto,
List<String>? tags}) =>
factory CloudinaryFile.fromUrl(
String url, {
CloudinaryResourceType resourceType: CloudinaryResourceType.Auto,
List<String>? tags,
String? folder,
}) =>
CloudinaryFile(
url: url,
identifier: url,
resourceType: resourceType,
folder: folder,
);

/// Convert [CloudinaryFile] to [MultipartFile]
Expand Down
4 changes: 4 additions & 0 deletions lib/src/cloudinary_public.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class CloudinaryPublic {
);
}

if (file.folder != null) {
data['folder'] = file.folder!;
}

if (file.tags != null && file.tags!.isNotEmpty) {
data['tags'] = file.tags!.join(',');
}
Expand Down
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -66,7 +66,7 @@ packages:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.1"
version: "0.13.3"
http_parser:
dependency: transitive
description:
Expand Down Expand Up @@ -113,7 +113,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -148,7 +148,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^0.13.1
http: ^0.13.3

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 992cb00

Please sign in to comment.