# UploadFS
**WARNING: This package will not receive any new features, only bug fixing when possible. The reason is that the Meteor package system is kind of obsolete now. I (the author of the package) plan to release a package on NPM that would work with other frameworks than Meteor, so it will be a full rewriting of UploadFS but with a completely different name. Thank you for your comprehension.**
UploadFS is a package for the Meteor framework that aims to make file uploading easy, fast and configurable.
Some important features are supported like the ability to **start, stop or even abort a transfer**, securing file access, transforming files on writing or reading...
If you want to support this package and feel graceful for all the work, please share this package with the community or feel free to send me pull requests if you want to contribute.
Also I'll be glad to receive donations, whatever you give it will be much appreciated.
If you find this lib useful and would like to contribute to it's development or just thank me (the author), donations are welcome.
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SS78MUMW8AH4N)
## Installation
To install the package, execute this command in the root of your project :
```
meteor add jalik:ufs
```
If later you want to remove the package :
```
meteor remove jalik:ufs
```
## Plugins
In this documentation, I am using the `UploadFS.store.Local` store which saves files on the filesystem.
But since the package is modular, you can install other stores or even create your own store.
* [UploadFS.store.Local](https://github.com/jalik/jalik-ufs-local)
* [UploadFS.store.GridFS](https://github.com/jalik/jalik-ufs-gridfs)
* [UploadFS.store.WABS](https://github.com/sebakerckhof/ufs-wabs)
* [UploadFS.store.S3](https://github.com/sebakerckhof/ufs-s3)
* [AutoForm-UFS](https://github.com/DesignmanIO/meteor-autoform-ufs)
## Testing
You can test the package by downloading and running [UFS-Example](https://github.com/jalik/ufs-example) which is simple demo of UploadFS.
## Mobile Testing
In order to test on mobile builds, `ROOT_URL` and `--mobile-server` must be set to your computer's local IP address and port:
```bash
export ROOT_URL=http://192.168.1.7:3000 && meteor run android-device --mobile-server=http://192.168.1.7:3000
```
## Configuration
You can access and modify settings via `UploadFS.config`.
```js
import {UploadFS} from 'meteor/jalik:ufs';
// Set default permissions for all stores (you can later overwrite the default permissions on each store)
UploadFS.config.defaultStorePermissions = new UploadFS.StorePermissions({
insert(userId, doc) {
return userId;
},
update(userId, doc) {
return userId === doc.userId;
},
remove(userId, doc) {
return userId === doc.userId;
}
});
// Use HTTPS in URLs
UploadFS.config.https = true;
// Activate simulation for slowing file reading
UploadFS.config.simulateReadDelay = 1000; // 1 sec
// Activate simulation for slowing file uploading
UploadFS.config.simulateUploadSpeed = 128000; // 128kb/s
// Activate simulation for slowing file writing
UploadFS.config.simulateWriteDelay = 2000; // 2 sec
// This path will be appended to the site URL, be sure to not put a "/" as first character
// for example, a PNG file with the _id 12345 in the "photos" store will be available via this URL :
// http://www.yourdomain.com/uploads/photos/12345.png
UploadFS.config.storesPath = 'uploads';
// Set the temporary directory where uploading files will be saved
// before sent to the store.
UploadFS.config.tmpDir = '/tmp/uploads';
// Set the temporary directory permissions.
UploadFS.config.tmpDirPermissions = '0700';
```
## Creating a Store (server)
**Since v0.6.7, you can share your store between client and server or define it on the server only.**
**Before v0.6.7, a store must be available on the client and the server.**
A store is the place where your files are saved, it could be your local hard drive or a distant cloud hosting solution.
Let say you have a `Photos` collection which is used to save the files info.
You need to create the store that will will contains the data of the `Photos` collection.
Note that the `name` of the store must be unique. In the following example we are using a local filesystem store.
Each store has its own options, so refer to the store documentation to see available options.
```js
import {Mongo} from 'meteor/mongo';
import {LocalStore} from 'meteor/jalik:ufs-local';
import {UploadFS} from 'meteor/jalik:ufs';
const Photos = new Mongo.Collection('photos');
const PhotoStore = new LocalStore({
collection: Photos,
name: 'photos',
path: '/uploads/photos'
});
```
## Filtering uploads (server)
### Filtering before uploading
You can filter uploads by assigning a `UploadFS.Filter` to a store.
The filter is tested before inserting a file in the collection.
If the file does not match the filter, it won't be inserted and will not be uploaded.
### Filtering after uploading
When the file is fully uploaded to the server, it's still in a temporary location.
At this moment you can validate the uploaded file before writing it to the store.
To do this, use `onValidate` option in the store options.
The following example shows a complete validation (before and after upload).
```js
import {Mongo} from 'meteor/mongo';
import {LocalStore} from 'meteor/jalik:ufs-local';
import {UploadFS} from 'meteor/jalik:ufs';
const Photos = new Mongo.Collection('photos');
const PhotoStore = new LocalStore({
collection: Photos,
name: 'photos',
path: '/uploads/photos',
// Apply a filter to restrict file upload
filter: new UploadFS.Filter({
minSize: 1,
maxSize: 1024 * 1000, // 1MB,
contentTypes: ['image/*'],
extensions: ['jpg', 'png']
}),
// Make sure that gallery pictures are indeed images
onValidate: function (file) {
// Here 'file' contains file metadata sent by the client, we need to get
// the disk path to the uploaded temp file to give it to gm.
const tempFilePath = UploadFS.getTempFilePath(file._id);
// Since the 'identify' function below is executed in a callback,
// we cannot directly throw an exception from it, because the exception would
// be catched by the server and not by ufs. We need to throw the exception
// from this onValidate function, so that ufs can catch the exception and
// trigger the cleanup process/report the error to the client.
// We do this by defining a future and waiting for it in onValidate, and having
// the future throw the exception instead of throwing it directly from
// the 'identify' function.
let future = new Future();
// identify will return an error if the content of the file is not an image.
// If it is an image, then details on the image file will be in data.
const identify = function (err, data) {
if (err) {
// Throw an exception to inform the client and trigger the cleanup process
future.throw(new Meteor.Error('not-an-image', 'The file is not an image'));
} else {
// We need to tell our future to return, or else it would stay stuck.
future.return();
}
};
gm(tempFilePath).identify(identify);
// Wait for 'identify' to complete, and either return or throw the exception
return future.wait();
}
});
```
If you need a more advanced filter, you can pass your own method using the `onCheck` option.
```js
import {Mongo} from 'meteor/mongo';
import {LocalStore} from 'meteor/jalik:ufs-local';
import {UploadFS} from 'meteor/jalik:ufs';
const Photos = new Mongo.Collection('photos');
const PhotoStore = new LocalStore({
collection: Photos,
name: 'photos',