active_storage 0.1.0

  • Readme
  • Changelog
  • Installing
  • 69

active_storage #

Send files to Rails Active Storage using direct upload.

Getting Started #

First, you instantiate an ActiveStorage object, passing it your Rails' backend url where ActiveStorage::DirectUploadsController (or your customization of it) is responding.

var activeStorage = ActiveStorage(
  directUploadURL: 'http://localhost:3000/rails/active_storage/direct_uploads',
);

Then, you just have to invoke the upload() method, passing it a File instance along with its info, like this:

DirectUploadResponse response = await activeStorage.upload(
  fileName: 'picture.jpg',
  fileMimeType: 'image/jpeg',
  file: File('my/picture.jpg'),
);

The returned DirectUploadResponse object provides a signedId getter. You should set that value in the API request attribute that your Rails backend will be setting on the ActiveRecord model attribute "powered by" ActiveStorage (i.e. annotated with has_one_attached).

Custom Headers #

It's very likely that your Rails backend's upload endpoint is accessible to authenticated users only. In that case, you'll need to pass an Authorization header together with the upload request. To do so, you just have to invoke the addHeader() method on the ActiveStorage instance, before invoking the upload() method. Like this:

activeStorage.addHeader(
  'Authorization',
  'Bearer ae38e518bfb4e2b11c2e5cefffe4581e66af07296cfec4b2299ab54a74ef3d8c',
);

Another case when you might want to set a custom header is for adding User-Agent information to your upload requests.

Upload Progress #

When uploading large files, it's important to be able to provide some feedback to your users about the progress. To do so, you have the option to pass a callback function to the upload() method, which would get called as many times as the number of byte chunks that are sent to the server, and every time would receive a double representing the upload progress percentage. Like this:

DirectUploadResponse response = await activeStorage.upload(
  fileName: 'large.jpg',
  fileMimeType: 'image/jpeg',
  file: File('pics/large.jpg'),
  onProgress: (percent) {
    print('Uploaded ${percent.toStringAsFixed(2)}%');
  },
);

[0.1.0] - November 26th, 2019 #

  • Initial release

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  active_storage: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:active_storage/active_storage.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because:

  • package:active_storage/active_storage.dart that imports:
  • package:active_storage/src/uploader.dart that imports:
  • dart:io

Package not compatible with runtime js

Because:

  • package:active_storage/active_storage.dart that imports:
  • package:active_storage/src/uploader.dart that imports:
  • dart:io

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and active_storage.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
crypto ^2.0.6 2.1.5
http ^0.12.0+2 0.12.1
meta ^1.1.8 1.2.2 1.3.0-nullsafety
Transitive dependencies
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
http_parser 3.1.4
path 1.7.0
pedantic 1.9.2
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
Dev dependencies
test ^1.6.0