Skip to content

Instantly share code, notes, and snippets.

@loongmxbt
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save loongmxbt/da2b361785ca6f9ad574 to your computer and use it in GitHub Desktop.
Save loongmxbt/da2b361785ca6f9ad574 to your computer and use it in GitHub Desktop.
Orion local filesystem based on cfs
orion.config.add('UPLOAD_PATH', 'filesystem');
var upload_path = orion.config.get('UPLOAD_PATH') || 'uploads';
// TODO: how to get folder name, how to use folder name?
// e.g. images from editor, folder is frola/images
var base = "";
if (Meteor.isServer) {
base = process.env.PWD;
}
Files = new FS.Collection("files", {
stores: [
new FS.Store.FileSystem("files", { path: base + '/public/' + upload_path })
],
filter: {
maxSize: 1000000,
allow: {
contentTypes: ['image/*']
}
}
});
Files.deny({
insert: function(){
return false;
},
update: function(){
return false;
},
remove: function(){
return false;
},
download: function(){
return false;
}
});
Files.allow({
insert: function(){
return true;
},
update: function(){
return true;
},
remove: function(){
return true;
},
download: function(){
return true;
}
});
orion.filesystem.providerUpload = function(options, success, failure) {
// Handle only single file upload
var file = options.fileList[0];
// If you want to upload different types of files to different FS.Collection,
// do extension check first
// var fileName = file.name;
// var isImage = (/\.(gif|jpg|jpeg|tiff|png)$/i).test(fileName);
// var isTorrent = (/\.(torrent)$/i).test(fileName);
// Use FS.Collection (Files) to upload the file
Files.insert(file, function(err, file) {
if (err) {
console.log('error', err);
} else {
// recreate upload file pattern
var fileName = file.collectionName + '-' + file._id + '-' + file.original.name;
var fileUrl = Meteor.absoluteUrl() + upload_path + '/' + fileName;
console.log(fileUrl);
var meta = {
cfs_id : file._id,
ext : file.extension()
};
success(fileUrl, meta);
}
});
}
orion.filesystem.providerRemove = function(file, success, failure) {
// Here "file" is an orion file object,
// Find the FS.File and delete it.
var cfs_id = file.meta.cfs_id;
// console.log(cfs_id);
// BUG: cannot find f sometime
var f = Files.findOne(cfs_id);
// console.log(f);
// remove record in cfs
Files.remove(f, function(err, file) {
if (err) {
console.log('error', err);
} else {
console.log('remove success');
success(); // remove record in orion
};
});
}
Package.describe({
name: 'orionjs:local-filesystem',
version: '0.0.1',
summary: 'Local storage for orion:filesystem',
git: '',
documentation: 'README.md'
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'orionjs:core@0.7.0',
'orionjs:filesystem@0.0.17',
'cfs:standard-packages',
'cfs:filesystem'
]);
api.addFiles('local-filesystem.js');
});
Package.onTest(function(api) {
api.use('tinytest');
api.use('orionjs:local-filesystem');
api.use('orionjs:filesystem');
api.addFiles('local-filesystem-tests.js');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment