2016-09-15 00:57:38 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc service
|
2016-09-16 03:21:34 +08:00
|
|
|
* @name passmanApp.FileService
|
2016-09-15 00:57:38 +08:00
|
|
|
* @description
|
2016-09-16 03:21:34 +08:00
|
|
|
* # FileService
|
2016-09-15 00:57:38 +08:00
|
|
|
* Service in the passmanApp.
|
|
|
|
*/
|
|
|
|
angular.module('passmanApp')
|
|
|
|
.service('FileService', ['$http', 'EncryptService', function ($http, EncryptService) {
|
|
|
|
return {
|
2016-10-05 20:21:46 +08:00
|
|
|
uploadFile: function (file, key) {
|
2016-09-15 00:57:38 +08:00
|
|
|
var queryUrl = OC.generateUrl('apps/passman/api/v2/file');
|
|
|
|
var _file = angular.copy(file);
|
2016-10-05 20:21:46 +08:00
|
|
|
_file.filename = EncryptService.encryptString(_file.filename, key);
|
|
|
|
var data = EncryptService.encryptString(angular.copy(file.data), key);
|
2016-09-15 00:57:38 +08:00
|
|
|
_file.data = data;
|
|
|
|
return $http.post(queryUrl, _file).then(function (response) {
|
2016-09-16 03:21:34 +08:00
|
|
|
if (response.data) {
|
2016-09-15 00:57:38 +08:00
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
deleteFile: function (file) {
|
2016-09-16 03:21:34 +08:00
|
|
|
var queryUrl = OC.generateUrl('apps/passman/api/v2/file/' + file.file_id);
|
2016-09-15 00:57:38 +08:00
|
|
|
var _file = angular.copy(file);
|
|
|
|
return $http.delete(queryUrl, _file).then(function (response) {
|
2016-09-16 03:21:34 +08:00
|
|
|
if (response.data) {
|
2016-09-15 00:57:38 +08:00
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-09-16 03:21:34 +08:00
|
|
|
getFile: function (file) {
|
|
|
|
var queryUrl = OC.generateUrl('apps/passman/api/v2/file/' + file.file_id);
|
|
|
|
var _file = angular.copy(file);
|
|
|
|
return $http.get(queryUrl, _file).then(function (response) {
|
|
|
|
if (response.data) {
|
|
|
|
if (Object.prototype.toString.call(response.data) === '[object Array]') {
|
|
|
|
return response.data.pop()
|
|
|
|
} else {
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-10-05 20:21:46 +08:00
|
|
|
/**
|
|
|
|
* Update a file and it's contents
|
|
|
|
* @param file
|
|
|
|
* @param key Optional encryption key to use
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
updateFile: function(file, key){
|
|
|
|
var queryUrl = OC.generateUrl('apps/passman/api/v2/file/'+ file.file_id);
|
|
|
|
var _file = angular.copy(file);
|
|
|
|
_file.filename = EncryptService.encryptString(_file.filename, key);
|
2016-10-05 22:58:19 +08:00
|
|
|
var data = EncryptService.encryptString(angular.copy(file.file_data), key);
|
|
|
|
_file.file_data = data;
|
2016-10-05 20:21:46 +08:00
|
|
|
return $http.patch(queryUrl, _file).then(function (response) {
|
|
|
|
if (response.data) {
|
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-09-16 03:21:34 +08:00
|
|
|
dataURItoBlob: function (dataURI, ftype) {
|
|
|
|
var byteString, mimeString, ab, ia, bb, i;
|
|
|
|
// convert base64 to raw binary data held in a string
|
|
|
|
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
|
|
|
|
byteString = atob(dataURI.split(',')[1]);
|
|
|
|
|
|
|
|
// separate out the mime component
|
|
|
|
mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
|
|
|
|
|
|
|
|
// write the bytes of the string to an ArrayBuffer
|
|
|
|
ab = new ArrayBuffer(byteString.length);
|
|
|
|
ia = new Uint8Array(ab);
|
|
|
|
for (i = 0; i < byteString.length; i++) {
|
|
|
|
ia[i] = byteString.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the ArrayBuffer to a blob, and you're done
|
|
|
|
bb = new Blob([ab], {
|
|
|
|
type: ftype
|
|
|
|
});
|
|
|
|
|
|
|
|
return URL.createObjectURL(bb);
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 00:57:38 +08:00
|
|
|
}]);
|