Files
cloudron-box/src/js/filemanager.js

295 lines
8.8 KiB
JavaScript
Raw Normal View History

2020-07-08 17:30:37 +02:00
'use strict';
2020-07-09 15:59:06 +02:00
/* global angular, $, async */
2020-07-08 17:30:37 +02:00
// create main application module
var app = angular.module('Application', ['angular-md5', 'ui-notification']);
angular.module('Application').filter('prettyOwner', function () {
return function (uid) {
2020-07-10 14:17:30 +02:00
if (uid === 0) return 'root';
2020-07-08 17:30:37 +02:00
if (uid === 33) return 'cloudron';
if (uid === 1000) return 'cloudron';
if (uid === 1001) return 'git';
2020-07-10 14:17:30 +02:00
return uid;
2020-07-08 17:30:37 +02:00
}
});
2020-07-10 15:27:44 +02:00
// disable sce for footer https://code.angularjs.org/1.5.8/docs/api/ng/service/$sce
app.config(function ($sceProvider) {
$sceProvider.enabled(false);
});
2020-07-08 17:30:37 +02:00
app.controller('FileManagerController', ['$scope', 'Client', function ($scope, Client) {
var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.split('='); }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {});
$scope.initialized = false;
2020-07-10 15:27:44 +02:00
$scope.status = null;
2020-07-08 17:30:37 +02:00
$scope.client = Client;
$scope.cwd = '/';
2020-07-10 15:01:56 +02:00
$scope.cwdParts = [];
2020-07-08 17:30:37 +02:00
$scope.appId = search.appId;
$scope.app = null;
$scope.entries = [];
function sanitize(filePath) {
filePath = filePath.split('/').filter(function (a) { return !!a; }).reduce(function (a, v) {
if (v === '.'); // do nothing
else if (v === '..') a.pop();
else a.push(v);
return a;
}, []).join('/');
return '/' + filePath;
}
$scope.refresh = function () {
Client.filesGet($scope.appId, $scope.cwd, function (error, result) {
if (error) return Client.error(error);
$scope.entries = result.entries;
});
};
$scope.uploadFile = function () {
console.log('uploadFile');
};
$scope.uploadFolder = function () {
console.log('uploadFolder');
};
$scope.open = function (entry) {
2020-07-09 16:26:23 +02:00
var filePath = sanitize($scope.cwd + '/' + entry.fileName);
2020-07-09 11:00:11 +02:00
2020-07-08 17:30:37 +02:00
if (entry.isDirectory) {
2020-07-09 11:00:11 +02:00
setDirectory(filePath);
2020-07-08 17:30:37 +02:00
} else if (entry.isFile) {
2020-07-09 11:00:11 +02:00
Client.filesGet($scope.appId, filePath, function (error, result) {
if (error) return Client.error(error);
console.log('open', result);
});
2020-07-08 17:30:37 +02:00
} else {}
};
$scope.goDirectoryUp = function () {
2020-07-09 11:00:11 +02:00
setDirectory($scope.cwd + '/..');
2020-07-08 17:30:37 +02:00
};
2020-07-10 15:01:56 +02:00
$scope.changeDirectory = function (path) {
setDirectory(path);
};
2020-07-08 17:30:37 +02:00
function setDirectory(path) {
path = sanitize(path);
if ($scope.cwd === path) return;
$scope.cwd = path;
2020-07-10 15:01:56 +02:00
$scope.cwdParts = path.split('/').filter(function (p) { return !!p; }).map(function (p, i) { return { name: p, path: path.split('/').slice(0, i+2).join('/') }; });
2020-07-08 17:30:37 +02:00
$scope.refresh();
}
2020-07-09 15:59:06 +02:00
$scope.uploadStatus = {
busy: false,
count: 0,
size: 0,
done: 0,
percentDone: 0
};
function uploadFiles(files) {
if (!files || !files.length) return;
$scope.uploadStatus.busy = true;
$scope.uploadStatus.count = files.length;
$scope.uploadStatus.size = 0;
$scope.uploadStatus.done = 0;
$scope.uploadStatus.percentDone = 0;
for (var i = 0; i < files.length; ++i) {
$scope.uploadStatus.size += files[i].size;
}
async.eachSeries(files, function (file, callback) {
var finishedUploadSize = $scope.uploadStatus.done;
var filePath = sanitize($scope.cwd + '/' + (file.webkitRelativePath || file.name));
Client.filesUpload($scope.appId, filePath, file, callback);
}, function (error) {
if (error) console.error(error);
$scope.uploadStatus.busy = false;
$scope.uploadStatus.count = 0;
$scope.uploadStatus.size = 0;
$scope.uploadStatus.done = 0;
$scope.uploadStatus.percentDone = 100;
$scope.refresh();
});
}
2020-07-09 17:59:33 +02:00
// file upload
$('#uploadFileInput').on('change', function (e) { uploadFiles(e.target.files || []); });
$scope.onUploadFile = function () { $('#uploadFileInput').click(); };
2020-07-09 15:59:06 +02:00
2020-07-09 17:59:33 +02:00
// folder upload
$('#uploadFolderInput').on('change', function (e ) { uploadFiles(e.target.files || []); });
$scope.onUploadFolder = function () { $('#uploadFolderInput').click(); };
2020-07-09 15:59:06 +02:00
2020-07-09 12:05:14 +02:00
$scope.newDirectory = {
busy: false,
error: null,
name: '',
show: function () {
$scope.newDirectory.error = null;
$scope.newDirectory.name = '';
$scope.newDirectory.busy = false;
2020-07-10 15:11:09 +02:00
$scope.newDirectoryForm.$setUntouched();
$scope.newDirectoryForm.$setPristine();
2020-07-09 12:05:14 +02:00
$('#newDirectoryModal').modal('show');
},
submit: function () {
$scope.newDirectory.busy = true;
2020-07-10 15:11:09 +02:00
$scope.newDirectory.error = null;
2020-07-09 12:05:14 +02:00
var filePath = sanitize($scope.cwd + '/' + $scope.newDirectory.name);
Client.filesCreateDirectory($scope.appId, filePath, function (error, result) {
$scope.newDirectory.busy = false;
2020-07-10 15:11:09 +02:00
if (error && error.statusCode === 409) return $scope.newDirectory.error = 'Already exists';
2020-07-09 12:05:14 +02:00
if (error) return Client.error(error);
console.log('create direcotory', result);
$scope.refresh();
$('#newDirectoryModal').modal('hide');
});
}
};
2020-07-09 12:59:29 +02:00
$scope.renameEntry = {
busy: false,
error: null,
entry: null,
newName: '',
show: function (entry) {
$scope.renameEntry.error = null;
$scope.renameEntry.entry = entry;
2020-07-09 16:26:23 +02:00
$scope.renameEntry.newName = entry.fileName;
2020-07-09 12:59:29 +02:00
$scope.renameEntry.busy = false;
$('#renameEntryModal').modal('show');
},
submit: function () {
$scope.renameEntry.busy = true;
2020-07-09 16:26:23 +02:00
var oldFilePath = sanitize($scope.cwd + '/' + $scope.renameEntry.entry.fileName);
2020-07-09 12:59:29 +02:00
var newFilePath = sanitize($scope.cwd + '/' + $scope.renameEntry.newName);
Client.filesRename($scope.appId, oldFilePath, newFilePath, function (error, result) {
$scope.renameEntry.busy = false;
if (error) return Client.error(error);
console.log('rename', result);
$scope.refresh();
$('#renameEntryModal').modal('hide');
});
}
};
2020-07-09 11:00:11 +02:00
$scope.entryRemove = {
busy: false,
error: null,
2020-07-09 12:59:29 +02:00
entry: null,
2020-07-09 11:00:11 +02:00
show: function (entry) {
$scope.entryRemove.error = null;
$scope.entryRemove.entry = entry;
$('#entryRemoveModal').modal('show');
},
submit: function () {
$scope.entryRemove.busy = true;
2020-07-09 16:26:23 +02:00
var filePath = sanitize($scope.cwd + '/' + $scope.entryRemove.entry.fileName);
2020-07-09 11:00:11 +02:00
Client.filesRemove($scope.appId, filePath, function (error, result) {
$scope.entryRemove.busy = false;
if (error) return Client.error(error);
console.log('remove', result);
$scope.refresh();
$('#entryRemoveModal').modal('hide');
});
}
};
2020-07-08 17:30:37 +02:00
function init() {
Client.getStatus(function (error, status) {
if (error) return Client.initError(error, init);
if (!status.activated) {
console.log('Not activated yet, redirecting', status);
window.location.href = '/';
return;
}
// check version and force reload if needed
if (!localStorage.version) {
localStorage.version = status.version;
} else if (localStorage.version !== status.version) {
localStorage.version = status.version;
window.location.reload(true);
}
2020-07-10 15:27:44 +02:00
$scope.status = status;
2020-07-08 17:30:37 +02:00
console.log('Running filemanager version ', localStorage.version);
// get user profile as the first thing. this populates the "scope" and affects subsequent API calls
Client.refreshUserInfo(function (error) {
if (error) return Client.initError(error, init);
2020-07-10 15:27:44 +02:00
Client.getApp($scope.appId, function (error, result) {
2020-07-08 17:30:37 +02:00
if (error) return Client.initError(error, init);
2020-07-10 15:27:44 +02:00
$scope.app = result;
2020-07-08 17:30:37 +02:00
2020-07-10 15:27:44 +02:00
// now mark the Client to be ready
Client.setReady();
2020-07-08 17:30:37 +02:00
2020-07-10 15:27:44 +02:00
$scope.refresh();
2020-07-08 17:30:37 +02:00
2020-07-10 15:27:44 +02:00
$scope.initialized = true;
2020-07-08 17:30:37 +02:00
});
});
});
}
init();
2020-07-10 15:11:09 +02:00
// setup all the dialog focus handling
['newDirectoryModal'].forEach(function (id) {
$('#' + id).on('shown.bs.modal', function () {
$(this).find("[autofocus]:first").focus();
});
});
2020-07-08 17:30:37 +02:00
}]);