Skip to content

Commit

Permalink
Changes version to 0.5.2
Browse files Browse the repository at this point in the history
Adds ufsImportURL method
Adds UploadFS.importFromURL(url, file, storeName, callback)
Moves meteor methods to ufs-methods.js
Fixes #34 (use an URL to save a file in a store)
Updates README
  • Loading branch information
jalik committed Feb 16, 2016
1 parent 8e1ac00 commit d327ef4
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 237 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ store.write(readStream, fileId, function(err, file) {

## Uploading files

### Uploading from a file

When the store on the server is configured, you can upload files to it.

Here is the template to upload one or more files :
Expand Down Expand Up @@ -391,6 +393,25 @@ Template.upload.events({
});
```

### Uploading from a 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.

```js
var url = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
var attributes = { name: 'Google Logo', description: 'Logo from www.google.com' };

UploadFS.importFromURL(url, attributes, PhotosStore, function (err, fileId) {
if (err) {
displayError(err);
} else {
console.log('Photo saved ', fileId);
}
});

```

## Displaying images

After that, if everything went good, you have you file saved to the store and in database.
Expand Down
3 changes: 2 additions & 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.1',
version: '0.5.2',
author: '[email protected]',
summary: 'Base package for UploadFS',
homepage: 'https://github.com/jalik/jalik-ufs',
Expand All @@ -24,6 +24,7 @@ Package.onUse(function (api) {
api.addFiles('ufs-store.js');
api.addFiles('ufs-helpers.js', 'client');
api.addFiles('ufs-uploader.js', 'client');
api.addFiles('ufs-methods.js', 'server');
api.addFiles('ufs-server.js', 'server');

api.export('UploadFS');
Expand Down
170 changes: 170 additions & 0 deletions ufs-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
Meteor.methods({

/**
* Completes the file transfer
* @param fileId
* @param storeName
*/
ufsComplete: function (fileId, storeName) {
check(fileId, String);
check(storeName, String);

// Allow other uploads to run concurrently
this.unblock();

var store = UploadFS.getStore(storeName);
if (!store) {
throw new Meteor.Error(404, 'store "' + storeName + '" does not exist');
}
// Check that file exists and is owned by current user
if (store.getCollection().find({_id: fileId, userId: this.userId}).count() < 1) {
throw new Meteor.Error(404, 'file "' + fileId + '" does not exist');
}

var fut = new Future();
var tmpFile = UploadFS.getTempFilePath(fileId);

// Get the temp file
var rs = fs.createReadStream(tmpFile, {
flags: 'r',
encoding: null,
autoClose: true
});

rs.on('error', Meteor.bindEnvironment(function () {
store.getCollection().remove(fileId);
}));

// Save file in the store
store.write(rs, fileId, Meteor.bindEnvironment(function (err, file) {
fs.unlink(tmpFile, function (err) {
err && console.error('ufs: cannot delete temp file ' + tmpFile + ' (' + err.message + ')');
});

if (err) {
fut.throw(err);
} else {
fut.return(file);
}
}));
return fut.wait();
},

/**
* Imports a file from the URL
* @param url
* @param file
* @param storeName
* @return {*}
*/
ufsImportURL: function (url, file, storeName) {
check(url, String);
check(file, Object);
check(storeName, String);

this.unblock();

var store = UploadFS.getStore(storeName);
if (!store) {
throw new Meteor.Error(404, 'Store "' + storeName + '" does not exist');
}

try {
// Extract file info
if (!file.name) {
file.name = url.replace(/\?.*$/, '').split('/').pop();
file.extension = file.name.split('.').pop();
file.type = 'image/' + file.extension;
}
// Check if file is valid
if (store.getFilter() instanceof UploadFS.Filter) {
store.getFilter().check(file);
}
// Create the file
var fileId = store.create(file);

} catch (err) {
throw new Meteor.Error(500, err.message);
}

var fut = new Future();
var proto;

// Detect protocol to use
if (/http:\/\//i.test(url)) {
proto = http;
} else if (/https:\/\//i.test(url)) {
proto = https;
}

// Download file
proto.get(url, Meteor.bindEnvironment(function (res) {
// Save the file in the store
store.write(res, fileId, function (err, file) {
if (err) {
fut.throw(err);
} else {
fut.return(fileId);
}
});
})).on('error', function (err) {
fut.throw(err);
});
return fut.wait();
},

/**
* Saves a chunk of file
* @param chunk
* @param fileId
* @param storeName
* @param progress
* @return {*}
*/
ufsWrite: function (chunk, fileId, storeName, progress) {
check(fileId, String);
check(storeName, String);
check(progress, Number);

this.unblock();

// Check arguments
if (!(chunk instanceof Uint8Array)) {
throw new Meteor.Error(400, 'chunk is not an Uint8Array');
}
if (chunk.length <= 0) {
throw new Meteor.Error(400, 'chunk is empty');
}

var store = UploadFS.getStore(storeName);
if (!store) {
throw new Meteor.Error(404, 'store ' + storeName + ' does not exist');
}

// Check that file exists, is not complete and is owned by current user
if (store.getCollection().find({_id: fileId, complete: false, userId: this.userId}).count() < 1) {
throw new Meteor.Error(404, 'file ' + fileId + ' does not exist');
}

var fut = new Future();
var tmpFile = UploadFS.getTempFilePath(fileId);

// Save the chunk
fs.appendFile(tmpFile, new Buffer(chunk), Meteor.bindEnvironment(function (err) {
if (err) {
console.error('ufs: cannot write chunk of file "' + fileId + '" (' + err.message + ')');
fs.unlink(tmpFile, function (err) {
err && console.error('ufs: cannot delete temp file ' + tmpFile + ' (' + err.message + ')');
});
fut.throw(err);
} else {
// Update completed state
store.getCollection().update(fileId, {
$set: {progress: progress}
});
fut.return(chunk.length);
}
}));
return fut.wait();
}
});
Loading

0 comments on commit d327ef4

Please sign in to comment.