parent
6d22b4decb
commit
488eced5a5
@ -1,78 +0,0 @@ |
||||
import http from 'http'; |
||||
import https from 'https'; |
||||
|
||||
import { Meteor } from 'meteor/meteor'; |
||||
import _ from 'underscore'; |
||||
|
||||
import { settings } from '../../../settings'; |
||||
import { FileUploadClass, FileUpload } from '../lib/FileUpload'; |
||||
import '../../ufs/Storj/server.js'; |
||||
|
||||
const get = function(file, req, res) { |
||||
const forceDownload = typeof req.query.download !== 'undefined'; |
||||
|
||||
this.store.getRedirectURL(file, forceDownload, (err, fileUrl) => { |
||||
if (err) { |
||||
return console.error(err); |
||||
} |
||||
|
||||
if (!fileUrl) { |
||||
return res.end(); |
||||
} |
||||
|
||||
return FileUpload.redirectToFile(fileUrl, req, res); |
||||
}); |
||||
}; |
||||
|
||||
const copy = function(file, out) { |
||||
const fileUrl = this.store.getRedirectURL(file); |
||||
|
||||
if (fileUrl) { |
||||
const request = /^https:/.test(fileUrl) ? https : http; |
||||
request.get(fileUrl, (fileRes) => fileRes.pipe(out)); |
||||
} else { |
||||
out.end(); |
||||
} |
||||
}; |
||||
|
||||
// This is the class that is retrieved when using FileUpload.getStoreByName / FileUpload.getStore
|
||||
const StorjUploads = new FileUploadClass({ |
||||
name: 'Storj:Uploads', |
||||
get, |
||||
copy, |
||||
// store setted bellow
|
||||
}); |
||||
|
||||
const StorjAvatars = new FileUploadClass({ |
||||
name: 'Storj:Avatars', |
||||
get, |
||||
copy, |
||||
// store setted bellow
|
||||
}); |
||||
|
||||
const StorjUserDataFiles = new FileUploadClass({ |
||||
name: 'Storj:UserDataFiles', |
||||
get, |
||||
copy, |
||||
// store setted bellow
|
||||
}); |
||||
|
||||
const configure = Meteor.bindEnvironment(_.debounce(function() { |
||||
const bucketName = settings.get('FileUpload_Storj_Bucket'); |
||||
const accessKey = settings.get('FileUpload_Storj_AccessKey'); |
||||
|
||||
if (!bucketName || !accessKey) { |
||||
return; |
||||
} |
||||
|
||||
const config = { |
||||
accessKey, |
||||
bucketName, |
||||
}; |
||||
|
||||
StorjUploads.store = FileUpload.configureUploadsStore('Storj', StorjUploads.name, config); |
||||
StorjAvatars.store = FileUpload.configureUploadsStore('Storj', StorjAvatars.name, config); |
||||
StorjUserDataFiles.store = FileUpload.configureUploadsStore('Storj', StorjUserDataFiles.name, config); |
||||
}, 500)); |
||||
|
||||
settings.get(/^FileUpload_Storj_/, configure); |
||||
@ -1,90 +0,0 @@ |
||||
import { check } from 'meteor/check'; |
||||
import { UploadFS } from 'meteor/jalik:ufs'; |
||||
import { Random } from 'meteor/random'; |
||||
// import uplink from 'uplink-js';
|
||||
|
||||
/** |
||||
* Storj store |
||||
* @param options |
||||
* @constructor |
||||
*/ |
||||
|
||||
export class StorjStore extends UploadFS.Store { |
||||
constructor(options) { |
||||
// Default options
|
||||
// options.accessKey,
|
||||
// options.bucketName,
|
||||
super(options); |
||||
|
||||
options.getPath = options.getPath || function(file) { |
||||
return file._id; |
||||
}; |
||||
|
||||
this.bucketName = options.bucketName; |
||||
|
||||
this.getPath = function(file) { |
||||
if (file.Storj) { |
||||
return file.Storj.path; |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* Creates the file in the collection |
||||
* @param file |
||||
* @param callback |
||||
* @return {string} |
||||
*/ |
||||
this.create = function(file, callback) { |
||||
check(file, Object); |
||||
|
||||
if (file._id == null) { |
||||
file._id = Random.id(); |
||||
} |
||||
|
||||
file.Storj = { |
||||
path: this.options.getPath(file), |
||||
}; |
||||
|
||||
file.store = this.options.name; // assign store to file
|
||||
return this.getCollection().insert(file, callback); |
||||
}; |
||||
|
||||
/** |
||||
* Removes the file |
||||
* @param fileId |
||||
* @param callback |
||||
*/ |
||||
this.delete = function(fileId, callback) { |
||||
this.project.deleteObject(this.bucketName, fileId).then(() => { |
||||
callback && callback(); |
||||
}); |
||||
}; |
||||
|
||||
/** |
||||
* Returns the file read stream |
||||
* @param fileId |
||||
* @param file |
||||
* @param options |
||||
* @return {*} |
||||
*/ |
||||
this.getReadStream = function(fileId/* , file, options = {}*/) { |
||||
const download = Promise.await(this.project.downloadObject(this.bucketName, fileId)); |
||||
return download.stream(); |
||||
}; |
||||
|
||||
/** |
||||
* Returns the file write stream |
||||
* @param fileId |
||||
* @param file |
||||
* @param options |
||||
* @return {*} |
||||
*/ |
||||
this.getWriteStream = function(fileId/* , file, options*/) { |
||||
const upload = Promise.await(this.project.uploadObject(this.bucketName, fileId)); |
||||
return upload.stream(); |
||||
}; |
||||
} |
||||
} |
||||
|
||||
// Add store to UFS namespace
|
||||
UploadFS.store.Storj = StorjStore; |
||||
Loading…
Reference in new issue