Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
djade007 committed May 23, 2020
0 parents commit 598751a
Show file tree
Hide file tree
Showing 10 changed files with 1,201 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/
.pubspec.lock

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: f7a6a7906be96d2288f5d63a5a54c515a6e987fe
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.0.1] - TODO: Add release date.

* TODO: Describe initial release.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# cloudinary_public

A dart wrapper for cloudinary public uploads

## Getting Started

This project is a starting point for a Dart
[package](https://flutter.dev/developing-packages/),
a library module containing code that can be shared easily across
multiple Flutter or Dart projects.

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions lib/cloudinary_public.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
library cloudinary_public;

import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

class CloudinaryPublic {
static const _baseUrl = 'https://api.cloudinary.com/v1_1';
Map<String, CloudinaryResponse> _uploadedFiles = {};
final String _cloudName;
final String _uploadPreset;
final bool cache;
Dio dioClient;

CloudinaryPublic(this._cloudName, this._uploadPreset,
{this.cache = false, this.dioClient}) {
// set default dio client
dioClient ??= Dio();
}

Future<CloudinaryResponse> uploadFile({
ByteData byteData,
File file,
CloudinaryResourceType resourceType: CloudinaryResourceType.Auto,
String filename,
}) async {
assert(
(byteData == null && file != null) ||
(byteData != null && file == null),
'only one between byteData or file must be provided');

if (cache) {
assert(filename != null, 'filename is required for caching');
if (_uploadedFiles.containsKey(filename)) return _uploadedFiles[filename];
}

FormData formData = FormData.fromMap({
'file': byteData != null
? MultipartFile.fromBytes(
byteData.buffer.asUint8List(),
filename: filename,
)
: MultipartFile.fromFile(file.path, filename: filename),
'upload_preset': _uploadPreset
});

// throws DioError
final res = await dioClient.post(
'$_baseUrl/$_cloudName/${describeEnum(resourceType).toLowerCase()}/upload',
data: formData,
);
final cloudinaryResponse = CloudinaryResponse.fromMap(res.data);

if (cache) { // temporary cache for this class instance
_uploadedFiles[filename] = cloudinaryResponse;
}
return cloudinaryResponse;
}
}

enum CloudinaryResourceType { Image, Raw, Video, Auto }

class CloudinaryResponse {
final String assetId;
final String publicId;
final DateTime createdAt;
final String url;
final String secureUrl;
final String originalFilename;

CloudinaryResponse({
this.assetId,
this.publicId,
this.createdAt,
this.url,
this.secureUrl,
this.originalFilename,
});

factory CloudinaryResponse.fromMap(Map<String, dynamic> data) {
return CloudinaryResponse(
assetId: data['asset_id'],
publicId: data['public_id'],
createdAt: DateTime.parse(data['created_at']),
url: data['url'],
secureUrl: data['secure_url'],
originalFilename: data['original_filename'],
);
}

Map<String, dynamic> toMap() {
return {
'asset_id': assetId,
'public_id': publicId,
'created_at': createdAt.toString(),
'url': url,
'secure_url': secureUrl,
'original_filename': originalFilename
};
}

@override
String toString() {
return toMap().toString();
}
}
Loading

0 comments on commit 598751a

Please sign in to comment.