Skip to content

Commit

Permalink
Changes package version to 0.5.4
Browse files Browse the repository at this point in the history
Updates README
Renames method arguments avoiding reserved words (from)
Handles errors on file reading to avoid server crashing
Handles cases where GET file request has no headers
  • Loading branch information
jalik committed Apr 18, 2016
1 parent ae4cd0f commit 74aba1e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ PhotosStore = new UploadFS.store.Local({

## Transforming files

If you need to modify the file before it is saved to the store, you have to use the **transform** option.
If you need to modify the file before it is saved to the store, you have to use the `transformRead` or `transformWrite` parameter.
A common use is to resize/compress images to get lighter versions of the uploaded files.

```js
Expand All @@ -131,14 +131,14 @@ PhotosStore = new UploadFS.store.Local({
name: 'photos',
path: '/uploads/photos',
// Transform file when reading
transformRead: function (from, to, fileId, file, request) {
from.pipe(to); // this returns the raw data
transformRead: function (readStream, writeStream, fileId, file, request) {
readStream.pipe(writeStream); // this returns the raw data
}
// Transform file when writing
transformWrite: function (from, to, fileId, file) {
transformWrite: function (readStream, writeStream, fileId, file) {
var im = Npm.require('imagemagick-stream');
var resize = im().resize('800x600').quality(90);
from.pipe(resize).pipe(to);
readStream.pipe(resize).pipe(writeStream);
}
});
```
Expand All @@ -158,10 +158,10 @@ PhotosStore = new UploadFS.store.Local({
new UploadFS.store.Local({
collection: Thumbnails,
name: 'thumbnails',
transformWrite: function(from, to, fileId, file) {
transformWrite: function(readStream, writeStream, fileId, file) {
var im = Npm.require('imagemagick-stream');
var resize = im().resize('128x128').quality(80);
from.pipe(resize).pipe(to);
readStream.pipe(resize).pipe(writeStream);
}
})
]
Expand Down Expand Up @@ -289,9 +289,28 @@ PhotosStore = new UploadFS.store.Local({
});
```

## Writing files
## Reading a file from a store

Sometimes you could need to write to the store directly on the server without any client involved.
If you need to get a file directly from a store, do like below :

```js
// Get the file from database
var file = Photos.findOne(fileId);

// Get the file stream from the store
var readStream = PhotosStore.getReadStream(fileId, file);

readStream.on('error', Meteor.bindEnvironment(function (error) {
console.error(err);
}));
readStream.on('data', Meteor.bindEnvironment(function (data) {
// handle the data
}));
```

## Writing a file to a store

If you need to save a file directly to a store, do like below :

```js
// Insert the file in database
Expand Down Expand Up @@ -395,7 +414,7 @@ Template.upload.events({
});
```

### Uploading from a URL
### Uploading from an URL

If you want to upload a file directly from a URL, use the `importFromURL(url, fileAttr, storeName, callback)` method.
This method is available both on the client and the server.
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: 'jalik:ufs',
version: '0.5.3',
version: '0.5.4',
author: '[email protected]',
summary: 'Base package for UploadFS',
homepage: 'https://github.com/jalik/jalik-ufs',
Expand Down
6 changes: 5 additions & 1 deletion ufs-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ WebApp.connectHandlers.use(function (req, res, next) {
ws.emit('end');
});

var accept = req.headers['accept-encoding'] || '';
var accept = '';
var headers = {
'Content-Type': file.type,
'Content-Length': file.size
};

if (typeof req.headers === 'object') {
accept = req.headers['accept-encoding'];
}

// Transform stream
store.transformRead(rs, ws, fileId, file, req, headers);

Expand Down
36 changes: 22 additions & 14 deletions ufs-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ UploadFS.Store = function (options) {
// Get original stream
var rs = self.getReadStream(fileId, file);

// Catch errors to avoid app crashing
rs.on('error', Meteor.bindEnvironment(function (error) {
callback.call(self, error, null);
}));

// Copy file data
store.write(rs, copyId, Meteor.bindEnvironment(function (err) {
if (err) {
Expand All @@ -160,33 +165,33 @@ UploadFS.Store = function (options) {

/**
* Transforms the file on reading
* @param from
* @param to
* @param readStream
* @param writeStream
* @param fileId
* @param file
* @param request
* @param headers
*/
self.transformRead = function (from, to, fileId, file, request, headers) {
self.transformRead = function (readStream, writeStream, fileId, file, request, headers) {
if (typeof transformRead === 'function') {
transformRead.call(self, from, to, fileId, file, request, headers);
transformRead.call(self, readStream, writeStream, fileId, file, request, headers);
} else {
from.pipe(to);
readStream.pipe(writeStream);
}
};

/**
* Transforms the file on writing
* @param from
* @param to
* @param readStream
* @param writeStream
* @param fileId
* @param file
*/
self.transformWrite = function (from, to, fileId, file) {
self.transformWrite = function (readStream, writeStream, fileId, file) {
if (typeof transformWrite === 'function') {
transformWrite.call(self, from, to, fileId, file);
transformWrite.call(self, readStream, writeStream, fileId, file);
} else {
from.pipe(to);
readStream.pipe(writeStream);
}
};

Expand All @@ -209,12 +214,15 @@ UploadFS.Store = function (options) {
ws.on('error', errorHandler);
ws.on('finish', Meteor.bindEnvironment(function () {
var size = 0;
var from = self.getReadStream(fileId, file);
var readStream = self.getReadStream(fileId, file);

from.on('data', function (data) {
readStream.on('error', Meteor.bindEnvironment(function (error) {
callback.call(self, error, null);
}));
readStream.on('data', Meteor.bindEnvironment(function (data) {
size += data.length;
});
from.on('end', Meteor.bindEnvironment(function () {
}));
readStream.on('end', Meteor.bindEnvironment(function () {
// Set file attribute
file.complete = true;
file.progress = 1;
Expand Down

0 comments on commit 74aba1e

Please sign in to comment.