Commit 58612cae authored by Sascha Pfeiffer's avatar Sascha Pfeiffer

some more progress

Signed-off-by: default avatarSascha Pfeiffer <[email protected]>
parent 924077e9
......@@ -96,7 +96,17 @@
}
if ($scope.bp.selected.hasOwnProperty('alternativeSave')) {
var onSuccess = function(){
// pass
};
var onError = function(error) {
$scope.errors.push(error);
};
$scope.bp.selected.alternativeSave($scope.bp.selected, parent, path, $uibModalInstance.close)
.then(onSuccess, onError);
} else {
$scope.bp.selected['callback_data'] = $scope.data;
$uibModalInstance.close($scope.bp.selected);
......
......@@ -2536,6 +2536,31 @@
return call(connection_type, endpoint, data, headers, session_secret_key);
};
/**
* @ngdoc
* @name psonocli.apiClient#read_shards
* @methodOf psonocli.apiClient
*
* @description
* Ajax GET request with the token as authentication to get the available shards and fileservers
*
* @param {string} token authentication token of the user, returned by authentication_login(email, authkey)
* @param {string} session_secret_key The session secret key
*
* @returns {promise} promise
*/
var read_shards = function (token, session_secret_key) {
var endpoint = '/shard/';
var connection_type = "GET";
var data = null;
var headers = {
"Authorization": "Token " + token
};
return call(connection_type, endpoint, data, headers, session_secret_key);
};
return {
info: info,
login: login,
......@@ -2614,7 +2639,8 @@
accept_membership: accept_membership,
decline_membership: decline_membership,
create_file: create_file,
delete_account: delete_account
delete_account: delete_account,
read_shards: read_shards
};
};
......
......@@ -28,17 +28,15 @@
return data;
};
var call = function(connection_type, endpoint, data, headers, session_secret_key, transformRequest) {
var call = function(fileserver_url, connection_type, endpoint, data, headers, session_secret_key, transformRequest) {
if (!transformRequest) {
transformRequest = $http.defaults.transformRequest;
}
var backend = 'https://browserplugins.chickahoona.com/fileserver01';
var req = {
method: connection_type,
url: backend + endpoint,
url: fileserver_url + endpoint,
data: data,
transformRequest: transformRequest
};
......@@ -69,6 +67,7 @@
* @description
* Ajax POST request to upload a file chunk
*
* @param {string} fileserver_url The url of the target fileserver
* @param {string} token The token of the user
* @param {Blob} chunk The content of the chunk to upload
* @param {string} ticket The ticket to authenticate the upload
......@@ -76,7 +75,7 @@
*
* @returns {promise} promise
*/
var upload = function (token, chunk, ticket, ticket_nonce) {
var upload = function (fileserver_url, token, chunk, ticket, ticket_nonce) {
var endpoint = '/upload/';
var connection_type = "POST";
......@@ -89,7 +88,7 @@
'Content-Type': undefined
};
return call(connection_type, endpoint, data, headers, '', angular.identity);
return call(fileserver_url, connection_type, endpoint, data, headers, '', angular.identity);
};
/**
......@@ -100,16 +99,18 @@
* @description
* Ajax GET request to get the server info
*
* @param {string} fileserver_url The url of the target fileserver
*
* @returns {promise} promise
*/
var info = function () {
var info = function (fileserver_url) {
var endpoint = '/info/';
var connection_type = "GET";
var data = null;
var headers = null;
return call(connection_type, endpoint, data, headers);
return call(fileserver_url, connection_type, endpoint, data, headers);
};
return {
......
......@@ -208,16 +208,15 @@
var file_chunk_size = 128*1024*1024; //128*1024*1024; // in bytes. e.g. 128*1024*1024 Bytes = 128 MB
var link_id = cryptoLibrary.generate_uuid();
var shard_id = 'd7054d0a-060f-46f9-abc8-31b206f8171d';
/**
* Uploads a file in chunks
*
* @param shard
* @param file
* @param file_id
* @param file_secret_key
*/
function multi_chunk_upload(file, file_id, file_secret_key) {
function multi_chunk_upload(shard, file, file_id, file_secret_key) {
var defer = $q.defer();
......@@ -237,7 +236,7 @@
console.log("blake2b " + chunk_position + " " + (new Date().getTime() - time_start)/1000);
time_start = new Date().getTime();
managerFileTransfer.upload(new Blob([encrypted_bytes], {type: 'application/octet-stream'}), file_id, chunk_position, shard_id, hash).then(function() {
managerFileTransfer.upload(new Blob([encrypted_bytes], {type: 'application/octet-stream'}), file_id, chunk_position, shard, hash).then(function() {
console.log("upload " + chunk_position + " " + (new Date().getTime() - time_start)/1000);
time_start = new Date().getTime();
resolve()
......@@ -286,6 +285,12 @@
return defer.promise;
}
/**
* Takes a dom field and returns the first file or nothing
*
* @param dom_field
* @returns {*}
*/
function get_dom_file(dom_field) {
if (dom_field.files.length <= 0) {
return;
......@@ -312,26 +317,33 @@
var dom_field = get_dom_file_field(selected.fields);
if (dom_field) {
var file = get_dom_file(dom_field);
if (!file) {
return;
}
selected.skipRegularCreate = true;
var chunk_count = Math.ceil(file.size / file_chunk_size);
var size = file.size;
if (!dom_field) {
return;
}
var file = get_dom_file(dom_field);
if (!file) {
return;
}
var parent_datastore_id = undefined;
var parent_share_id = undefined;
var onSuccess = function(shards){
if (parent.hasOwnProperty("share_id")) {
parent_share_id = parent.share_id;
} else if(parent.hasOwnProperty("datastore_id")) {
parent_datastore_id = parent.datastore_id;
shards = managerFileTransfer.filter_shards(shards, null, true);
if (shards.length < 1) {
return $q.reject('NO_FILESERVER_AVAILABLE')
}
var shard;
if (shards.length > 1) {
// TODO open modal to select target shard
} else {
shard = shards[0]
}
var onSuccess = function(file_id){
multi_chunk_upload(file, file_id, file_secret_key).then(function() {
multi_chunk_upload(shard, file, file_id, file_secret_key).then(function() {
console.log("multi_chunk_upload resolved");
})
};
......@@ -340,11 +352,28 @@
//pass
};
managerFileTransfer.create_file(shard_id, size + chunk_count * 40, chunk_count, link_id, parent_datastore_id, parent_share_id)
var chunk_count = Math.ceil(file.size / file_chunk_size);
var size = file.size;
var parent_datastore_id = undefined;
var parent_share_id = undefined;
if (parent.hasOwnProperty("share_id")) {
parent_share_id = parent.share_id;
} else if(parent.hasOwnProperty("datastore_id")) {
parent_datastore_id = parent.datastore_id;
}
managerFileTransfer.create_file(shard['shard_id'], size + chunk_count * 40, chunk_count, link_id, parent_datastore_id, parent_share_id)
.then(onSuccess, onError);
}
};
selected.skipRegularCreate = true;
var onError = function() {
//pass
};
return managerFileTransfer.read_shards()
.then(onSuccess, onError);
}
};
......
......@@ -5,6 +5,7 @@
* @ngdoc service
* @name psonocli.managerFileTransfer
* @requires $q
* @requires psonocli.helper
* @requires psonocli.managerBase
* @requires psonocli.cryptoLibrary
* @requires psonocli.apiClient
......@@ -14,7 +15,7 @@
* Service to manage everything around file transfer
*/
var managerFileTransfer = function($q, managerBase, cryptoLibrary , apiClient, apiFileserver) {
var managerFileTransfer = function($q, helper, managerBase, cryptoLibrary , apiClient, apiFileserver) {
/**
......@@ -63,22 +64,30 @@
* @param {Blob} chunk The content of the chunk to upload
* @param {uuid} file_id The target id of the file this chunk belongs to
* @param {int} chunk_position The sequence number of the chunk to determine the order
* @param {uuid} shard_id The target shard ID
* @param {uuid} shard The target shard
* @param {string} hash_blake2b The blake2b hash
*
* @returns {promise} promise
*/
var upload = function (chunk, file_id, chunk_position, shard_id, hash_blake2b) {
var upload = function (chunk, file_id, chunk_position, shard, hash_blake2b) {
var ticket = {
'file_id': file_id,
'chunk_position': chunk_position,
'shard_id': shard_id,
'shard_id': shard['shard_id'],
'hash_blake2b': hash_blake2b
};
var ticket_enc = cryptoLibrary.encrypt_data(JSON.stringify(ticket), managerBase.get_session_secret_key());
var fileserver;
if (shard['fileserver'].length > 1) {
// math random should be good enough here, don't use for crypto!
var pos = Math.floor(Math.random() * shard['fileserver'].length);
fileserver = shard['fileserver'][pos];
} else {
fileserver = shard['fileserver'][0];
}
var onError = function(result) {
return $q.reject(result.data)
......@@ -88,18 +97,86 @@
return result.data;
};
return apiFileserver.upload(managerBase.get_token(), chunk, ticket_enc.text, ticket_enc.nonce)
return apiFileserver.upload(fileserver['fileserver_url'], managerBase.get_token(), chunk, ticket_enc.text, ticket_enc.nonce)
.then(onSuccess, onError);
};
/**
* @ngdoc
* @name psonocli.managerFileTransfer#read_shard
* @methodOf psonocli.managerFileTransfer
*
* @description
* Triggered once someone wants to retrieve the potential shards from the server
*
* @returns {promise} promise with all the shards
*/
var read_shards = function () {
var onError = function(result) {
return $q.reject(result.data)
};
var onSuccess = function(result) {
return result.data['shards']
};
return apiClient.read_shards(managerBase.get_token(),
managerBase.get_session_secret_key())
.then(onSuccess, onError);
};
/**
* @ngdoc
* @name psonocli.managerFileTransfer#filter_shards
* @methodOf psonocli.managerFileTransfer
*
* @description
* Filters an array of shards and fileservers
*
* @param {array} shards Array of shards
* @param {bool} require_read Determines whether read is required or not
* @param {bool} require_write Determines whether write is required or not
*
* @returns {array} list of shards with fileservers that fulfill the filter criteria
*/
var filter_shards = function (shards, require_read, require_write) {
var filtered_shards = [];
for (var i = 0; i < shards.length; i++) {
if (require_read && !shards[i]['read']) {
continue
}
if (require_write && !shards[i]['write']) {
continue
}
var filtered_shard = angular.copy(shards[i]);
filtered_shards.push(filtered_shard);
helper.remove_from_array(filtered_shard['fileserver'], undefined, function(fileserver, nothing) {
return (require_read && !fileserver['read']) || (require_write && !fileserver['write'])
})
}
return filtered_shards;
};
return {
create_file: create_file,
upload: upload
upload: upload,
read_shards: read_shards,
filter_shards: filter_shards
};
};
var app = angular.module('psonocli');
app.factory("managerFileTransfer", ['$q', 'managerBase', 'cryptoLibrary', 'apiClient', 'apiFileserver', managerFileTransfer]);
app.factory("managerFileTransfer", ['$q', 'helper', 'managerBase', 'cryptoLibrary', 'apiClient', 'apiFileserver', managerFileTransfer]);
}(angular));
\ No newline at end of file
{
"NO_FILESERVER_AVAILABLE": "Kein Fileserver verfügbar",
"FILE_SIZE": "Dateigröße",
"FILE_SIZE_BYTES": "Dateigröße (in Bytes)",
"ENTRY_LINK": "Link zum Eintrag",
......
{
"NO_FILESERVER_AVAILABLE": "No fileserver available",
"FILE_SIZE": "File Size",
"FILE_SIZE_BYTES": "File Size (in bytes)",
"ENTRY_LINK": "Link to the entry",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment