Skip to content

Commit

Permalink
Add folder names, public id
Browse files Browse the repository at this point in the history
  • Loading branch information
lukkob committed Jan 24, 2021
1 parent c96df90 commit 8fbae16
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 50 deletions.
4 changes: 2 additions & 2 deletions example/lib/example.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'dart:io';

import 'package:cloudinary_public/cloudinary_public.dart';
import 'package:cloudinary/cloudinary.dart';
import 'package:flutter/services.dart';

main() async {
// set cache as true if you don't want to make an upload call with files of the same filename
// in such case if the filepath/identifier has already been uploaded before, you simply get the previously cached response.
var cloudinary =
CloudinaryPublic('CLOUD_NAME', 'UPLOAD_PRESET', cache: false);
Cloudinary('CLOUD_NAME', 'UPLOAD_PRESET', cache: false);

// Using a file. For example, gotten from: https://pub.dev/packages/image_picker
File file = File('');
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
cloudinary_public:
cloudinary:
dependency: "direct dev"
description:
path: ".."
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dev_dependencies:
flutter_test:
sdk: flutter

cloudinary_public:
cloudinary:
path: ../

# For information on the generic Dart part of this file, see the
Expand Down
4 changes: 2 additions & 2 deletions lib/cloudinary_public.dart → lib/cloudinary.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library cloudinary_public;
library cloudinary;

export 'src/cloudinary_file.dart';
export 'src/cloudinary_public.dart';
export 'src/cloudinary.dart';
export 'src/cloudinary_resource_type.dart';
export 'src/cloudinary_response.dart';
export 'src/transformation/cloudinary_image.dart';
33 changes: 25 additions & 8 deletions lib/src/cloudinary_public.dart → lib/src/cloudinary.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:cloudinary_public/cloudinary_public.dart';
import 'package:cloudinary/cloudinary.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

import './cloudinary_response.dart';
import '../cloudinary_public.dart';
import '../cloudinary.dart';

/// The base class for this package
class CloudinaryPublic {
class Cloudinary {
/// Cloudinary api base url
static const _baseUrl = 'https://api.cloudinary.com/v1_1';

Expand All @@ -25,7 +25,7 @@ class CloudinaryPublic {
/// The Dio client to be used to upload files
Dio dioClient;

CloudinaryPublic(
Cloudinary(
this._cloudName,
this._uploadPreset, {
this.cache = false,
Expand All @@ -43,19 +43,22 @@ class CloudinaryPublic {
Future<List<CloudinaryResponse>> uploadFiles(
List<CloudinaryFile> files, {
String uploadPreset,
List<String> folderNames,
}) {
return Future.wait(
files.map(
(file) => uploadFile(file, uploadPreset: uploadPreset),
(file) => uploadFile(file,
uploadPreset: uploadPreset, folderNames: folderNames),
),
);
}

/// Upload the cloudinary file to using the public api
/// Override the default upload preset (when [CloudinaryPublic] is instantiated) with this one (if specified).
/// Override the default upload preset (when [Cloudinary] is instantiated) with this one (if specified).
Future<CloudinaryResponse> uploadFile(
CloudinaryFile file, {
String uploadPreset,
List<String> folderNames,
}) async {
if (cache) {
assert(file.identifier != null, 'identifier is required for caching');
Expand All @@ -69,6 +72,16 @@ class CloudinaryPublic {
'upload_preset': uploadPreset ?? _uploadPreset,
});

if (folderNames != null && folderNames.isNotEmpty) {
formData.fields
.add(MapEntry<String, String>('folder', folderNames.join('/')));
formData.fields.add(MapEntry<String, String>(
'public_id', folderNames.join('/') + '/' + file.identifier));
} else {
formData.fields
.add(MapEntry<String, String>('public_id', file.identifier));
}

if (file.tags != null && file.tags.isNotEmpty) {
formData.fields
.add(MapEntry<String, String>('tags', file.tags.join(',')));
Expand All @@ -95,18 +108,22 @@ class CloudinaryPublic {
Future<CloudinaryResponse> uploadFutureFile(
Future<CloudinaryFile> file, {
String uploadPreset,
List<String> folderNames,
}) async {
return uploadFile(await file, uploadPreset: uploadPreset);
return uploadFile(await file,
uploadPreset: uploadPreset, folderNames: folderNames);
}

/// Upload multiple files using simultaneously [uploadFutureFile]
Future<List<CloudinaryResponse>> multiUpload(
List<Future<CloudinaryFile>> files, {
String uploadPreset,
List<String> folderNames,
}) async {
return Future.wait(
files.map(
(file) => uploadFutureFile(file, uploadPreset: uploadPreset),
(file) => uploadFutureFile(file,
uploadPreset: uploadPreset, folderNames: folderNames),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cloudinary_file.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:cloudinary_public/cloudinary_public.dart';
import 'package:cloudinary/cloudinary.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/transformation/transformation.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../../cloudinary_public.dart';
import '../../cloudinary.dart';

class Transformation {
final String _path;
Expand Down
48 changes: 24 additions & 24 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ packages:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "7.0.0"
version: "12.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.39.17"
version: "0.40.6"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -42,7 +42,7 @@ packages:
name: build
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
version: "1.6.1"
built_collection:
dependency: transitive
description:
Expand Down Expand Up @@ -91,7 +91,7 @@ packages:
name: code_builder
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
version: "3.6.0"
collection:
dependency: transitive
description:
Expand All @@ -113,20 +113,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.2"
dart_style:
dependency: transitive
description:
name: dart_style
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.6"
version: "1.3.10"
dio:
dependency: "direct main"
description:
Expand All @@ -141,6 +134,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "5.2.1"
fixnum:
dependency: transitive
description:
Expand All @@ -165,20 +165,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
html:
http_parser:
dependency: transitive
description:
name: html
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+4"
http_parser:
version: "3.1.4"
intl:
dependency: transitive
description:
name: http_parser
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
version: "0.16.1"
js:
dependency: transitive
description:
Expand Down Expand Up @@ -213,21 +213,21 @@ packages:
name: mockito
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.2"
version: "4.1.3"
node_interop:
dependency: transitive
description:
name: node_interop
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
version: "1.2.1"
node_io:
dependency: transitive
description:
name: node_io
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
version: "1.2.0"
package_config:
dependency: transitive
description:
Expand Down Expand Up @@ -262,7 +262,7 @@ packages:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
version: "2.1.5"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -274,7 +274,7 @@ packages:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+1"
version: "0.9.10+1"
source_span:
dependency: transitive
description:
Expand Down Expand Up @@ -346,4 +346,4 @@ packages:
source: hosted
version: "2.2.1"
sdks:
dart: ">=2.10.0-110 <2.11.0"
dart: ">=2.10.0 <2.11.0"
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: cloudinary_public
name: cloudinary
description: This package allows you to upload media files directly to cloudinary, without exposing your apiKey or secretKey.
version: 0.6.2
homepage: https://github.com/djade007/cloudinary_public
homepage: https://github.com/lukkob/cloudinary_sdk

environment:
sdk: ">=2.7.0 <3.0.0"
Expand Down
16 changes: 8 additions & 8 deletions test/cloudinary_public_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:cloudinary_public/cloudinary_public.dart';
import 'package:cloudinary/cloudinary.dart';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -38,7 +38,7 @@ void main() {
);

test('uploads an image from external url', () async {
final cloudinary = CloudinaryPublic(
final cloudinary = Cloudinary(
cloudName,
uploadPreset,
dioClient: client,
Expand All @@ -65,7 +65,7 @@ void main() {
final tempFile = getFile();

test('uploads an image file', () async {
final cloudinary = CloudinaryPublic(
final cloudinary = Cloudinary(
cloudName,
uploadPreset,
dioClient: client,
Expand All @@ -87,7 +87,7 @@ void main() {
});

test('uploads an image file overriding the upload_preset', () async {
final cloudinary = CloudinaryPublic(
final cloudinary = Cloudinary(
cloudName,
uploadPreset,
dioClient: client,
Expand All @@ -112,7 +112,7 @@ void main() {
});

test('upload multiple image files', () async {
final cloudinary = CloudinaryPublic(
final cloudinary = Cloudinary(
cloudName,
uploadPreset,
dioClient: client,
Expand All @@ -136,7 +136,7 @@ void main() {
});

test('upload multiple image byteData', () async {
final cloudinary = CloudinaryPublic(
final cloudinary = Cloudinary(
cloudName,
uploadPreset,
dioClient: client,
Expand All @@ -160,7 +160,7 @@ void main() {
});

test('Test transformation', () {
final cloudinary = CloudinaryPublic(
final cloudinary = Cloudinary(
cloudName,
uploadPreset,
dioClient: client,
Expand Down Expand Up @@ -201,7 +201,7 @@ void main() {
});

test('thumbnail shortcut', () {
final cloudinary = CloudinaryPublic(
final cloudinary = Cloudinary(
cloudName,
uploadPreset,
dioClient: client,
Expand Down

0 comments on commit 8fbae16

Please sign in to comment.