-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from prey/filebrowser-upload
Filebrowser permissions
- Loading branch information
Showing
5 changed files
with
180 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/usr/bin/env node | ||
|
||
var fs = require('fs'), | ||
path = require('path'), | ||
mime = require('mime'), | ||
common = require('./../../common'), | ||
needle = require('needle'); | ||
|
||
var config = common.config, | ||
protocol = config._values['control-panel'].protocol, | ||
host = config._values['control-panel'].host, | ||
url = protocol + '://' + host; | ||
|
||
var UPLOAD_SERVER = url + '/upload/upload', | ||
RESUMABLE_HEADER = 'X-Prey-Upload-Resumable', | ||
OPEN_TIMEOUT = 180000, | ||
READ_TIMEOUT = 2000; | ||
|
||
var PATH = 2, | ||
USER = 3, | ||
NAME = 4, | ||
SIZE = 5, | ||
FILE_ID = 6, | ||
RESUME = 7, | ||
PORT = 8; | ||
|
||
function main() { | ||
var argv = process.argv; | ||
var options = { | ||
path: argv[PATH], | ||
user: argv[USER], | ||
name: argv[NAME], | ||
size: argv[SIZE], | ||
file_id: argv[FILE_ID], | ||
resumable : argv[RESUME], | ||
port: argv[PORT] | ||
} | ||
Main(options, function(err) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
}); | ||
} | ||
|
||
function Main(options, cb) { | ||
var file_path = options.path, | ||
file_id = options.file_id, | ||
file_size = parseInt(options.size), | ||
file_name = options.name, | ||
user = options.user; | ||
|
||
console.log("Uploading file: ", file_path, file_id); | ||
|
||
var file = { | ||
total: 0, | ||
path: file_path, | ||
user: user, | ||
id: file_id, | ||
size: file_size, | ||
resumable: options.resumable | ||
} | ||
retrieve_file(file, cb); | ||
} | ||
|
||
function retrieve_file(file, cb) { | ||
if (file.resumable == 'true') { | ||
console.log("Resumable file:", file.id); | ||
var url = UPLOAD_SERVER + '?uploadID=' + file.id; | ||
// Make a call to get the last byte processed by the upload server | ||
// in order to resume the upload from that position. | ||
needle.request('get', url, null, function(err, res) { | ||
if (err) { | ||
console.log(err); | ||
cb(err); | ||
return; | ||
} | ||
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) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
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) { | ||
console.log(err); | ||
cb(err); | ||
return; | ||
} | ||
var out = res.statusCode; | ||
if (out !== 200 && out !== 201) { | ||
var err = new Error('There was an error communicating with the server'); | ||
cb(err); | ||
return; | ||
} | ||
console.log("File succesfuly uploaded:", file.id); | ||
cb(null); // delete files | ||
}) | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters