-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filebrowser dev #184
Filebrowser dev #184
Changes from 36 commits
a5fbfeb
769baf8
e2210c2
06e27a5
2a5391c
aa7643f
d951852
846b42d
01da418
ef5dce4
1acc3d1
cd62ef6
9cf7dd2
4654414
6e57df5
2516a83
32b167b
2a058db
677efe6
cd4f134
3e9ff2c
2be5e52
d54940d
ddff15b
9914029
b60a1f3
1b2f03a
078429d
bcafcd7
8c888f3
f9a34d5
6a15488
bc3673a
e220473
533fd3a
ba7b23c
774273b
869eda3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var join = require('path').join, | ||
base_path = join(__dirname, '..', '..'), | ||
common = require('./../../common'), | ||
hooks = require('./../../hooks'), | ||
triggers = require('./../../triggers'), | ||
storage = require('./../../utils/storage'), | ||
fileretrieval = require(join(base_path, 'actions', 'fileretrieval')); | ||
|
||
var logger = common.logger; | ||
watching = false; | ||
|
||
var NAMESPACE = "fr/"; | ||
|
||
var exist = function(id, cb) { | ||
var key = NAMESPACE + id; | ||
storage.all(function(err, files) { | ||
if (err) | ||
return logger.error(err.message); | ||
if (files[key]) | ||
return cb(true); | ||
return cb(false); | ||
}) | ||
} | ||
|
||
exports.store = function(id, path, size, user) { | ||
exist(id, function(cb) { | ||
if (cb == false) { | ||
logger.debug('Storing file_id in DB: ' + id); | ||
var opts = { | ||
path: path, | ||
size: size, | ||
user: user | ||
} | ||
storage.set(NAMESPACE + id, opts); | ||
} | ||
}); | ||
} | ||
|
||
exports.del = function(id) { | ||
logger.debug('Removing file_id from DB: ' + id); | ||
storage.del(NAMESPACE + id); | ||
} | ||
|
||
exports.run_stored = function(cb) { | ||
storage.all(function(err, files) { | ||
if (err) | ||
return logger.error(err.message); | ||
|
||
var count = Object.keys(files).length; | ||
if (count <= 0) | ||
return; | ||
|
||
logger.warn('Re-uploading ' + count + ' pending files.'); | ||
|
||
for (key in files) { | ||
if(key.indexOf(NAMESPACE) != 0) { | ||
continue; | ||
} | ||
var opts = { | ||
file_id: key.substring(3, key.length), | ||
path: files[key].path, | ||
user: files[key].user, | ||
size: files[key].size, | ||
resumable: true | ||
} | ||
fileretrieval.start(opts, cb); | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
"use strict"; | ||
|
||
////////////////////////////////////////// | ||
// Prey JS FileRetrieval | ||
// (C) 2016 Prey, Inc. | ||
// by Mauricio Schneider and Javier Acuña - http://preyproject.com | ||
// GPLv3 Licensed | ||
////////////////////////////////////////// | ||
|
||
var fs = require('fs'), | ||
path = require('path'), | ||
mime = require('mime'), | ||
needle = require('needle'), | ||
join = require('path').join, | ||
api = require('./../../plugins/control-panel/api'), | ||
files = require('./files_storage'), | ||
EventEmitter = require('events').EventEmitter; | ||
|
||
var common = require('./../../common'), | ||
system = common.system, | ||
config = common.config, | ||
protocol = config._values['control-panel'].protocol, | ||
host = config._values['control-panel'].host, | ||
url = protocol + '://' + host, | ||
node_bin = path.join(system.paths.current, 'bin', 'node'), | ||
logger = common.logger, | ||
os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows'); | ||
|
||
var em, | ||
cp; | ||
|
||
var UPLOAD_SERVER = url + '/upload/upload', | ||
RESUMABLE_HEADER = 'X-Prey-Upload-Resumable', | ||
OPEN_TIMEOUT = 180000, | ||
READ_TIMEOUT = 5000; | ||
|
||
exports.check_pending_files = function() { | ||
files.run_stored(); | ||
} | ||
|
||
exports.start = function(options, cb) { | ||
var file_path = options.path, | ||
file_id = options.file_id, | ||
file_size = parseInt(options.size), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value should already be a int, so callers of filetrieval.start should provide a numeric value for file_size |
||
user = options.user; | ||
|
||
em = em || new EventEmitter(); | ||
if (cb) cb(null, em); | ||
|
||
if (!options.resumable) | ||
files.store(file_id, file_path, file_size, user); | ||
|
||
var file = { | ||
total: 0, | ||
path: file_path, | ||
user: user, | ||
id: file_id, | ||
size: file_size, | ||
resumable: options.resumable | ||
} | ||
|
||
retrieve_file(file, function() { | ||
files.del(file.id); | ||
}); | ||
} | ||
|
||
function retrieve_file(file, cb) { | ||
if (file.resumable) { | ||
var url = UPLOAD_SERVER + '?uploadID=' + file.id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment describing the protocol (for the community), saying that this call basically gets the last byte processed by the upload server in order to resume the upload from that position |
||
// Make a call to get the last byte processed by the upload server | ||
// before the disconnection in order to resume the upload from that position | ||
needle.request('get', url, null, function(err, res) { | ||
if (err) return em.emit(err); | ||
var data = JSON.parse(res.body); | ||
file.total = data.Total; | ||
get_file(file, cb); | ||
}) | ||
return; | ||
} | ||
get_file(file, cb); | ||
} | ||
|
||
function get_file(file, cb) { | ||
var buffsize = (file.size == 0) ? 1 : (file.size - file.total); | ||
var buf = new Buffer(buffsize); | ||
var fd = fs.openSync(file.path, "r"); | ||
|
||
fs.read(fd, buf, 0, file.size - file.total, file.total, function(err, read, buf) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. process the err
|
||
if (err) return em.emit(err); | ||
upload_file(file, buf, cb); | ||
}) | ||
} | ||
|
||
function upload_file(file, buf, cb) { | ||
var options = { | ||
open_timeout: OPEN_TIMEOUT, | ||
read_timeout: READ_TIMEOUT | ||
}; | ||
|
||
if (file.total > 0) { | ||
RESUMABLE_HEADER = file.total; | ||
} | ||
var url = UPLOAD_SERVER + '?uploadID='+file.id; | ||
needle.post(url, buf, options, function(err, res) { | ||
if (err) { | ||
logger.error(err) | ||
return em.emit(err); | ||
} | ||
var out = res.statusCode; | ||
if (out !== 200 && out !== 201) { | ||
var err = new Error('There was an error communicating with the server'); | ||
logger.error(err) | ||
em.emit(err); | ||
} | ||
cb(); | ||
}) | ||
em.emit('end'); | ||
} | ||
|
||
exports.stop = function() { | ||
if (cp && !cp.exitCode) { | ||
cp.kill(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all require calls should be in one var group and non require calls should be in a different var group