'use strict'; /* global angular, $, async */ // create main application module var app = angular.module('Application', ['angular-md5', 'ui-notification']); angular.module('Application').filter('prettyOwner', function () { return function (uid) { if (uid === 0) return 'root'; if (uid === 33) return 'cloudron'; if (uid === 1000) return 'cloudron'; if (uid === 1001) return 'git'; return uid; } }); 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; $scope.client = Client; $scope.cwd = '/'; $scope.cwdParts = []; $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); console.log(result); $scope.entries = result.entries; }); }; $scope.uploadFile = function () { console.log('uploadFile'); }; $scope.uploadFolder = function () { console.log('uploadFolder'); }; $scope.open = function (entry) { var filePath = sanitize($scope.cwd + '/' + entry.fileName); if (entry.isDirectory) { setDirectory(filePath); } else if (entry.isFile) { Client.filesGet($scope.appId, filePath, function (error, result) { if (error) return Client.error(error); console.log('open', result); }); } else {} }; $scope.goDirectoryUp = function () { setDirectory($scope.cwd + '/..'); }; $scope.changeDirectory = function (path) { setDirectory(path); }; function setDirectory(path) { path = sanitize(path); if ($scope.cwd === path) return; $scope.cwd = path; $scope.cwdParts = path.split('/').filter(function (p) { return !!p; }).map(function (p, i) { return { name: p, path: path.split('/').slice(0, i+2).join('/') }; }); $scope.refresh(); } $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(); }); } // file upload $('#uploadFileInput').on('change', function (e) { uploadFiles(e.target.files || []); }); $scope.onUploadFile = function () { $('#uploadFileInput').click(); }; // folder upload $('#uploadFolderInput').on('change', function (e ) { uploadFiles(e.target.files || []); }); $scope.onUploadFolder = function () { $('#uploadFolderInput').click(); }; $scope.newDirectory = { busy: false, error: null, name: '', show: function () { $scope.newDirectory.error = null; $scope.newDirectory.name = ''; $scope.newDirectory.busy = false; $scope.newDirectoryForm.$setUntouched(); $scope.newDirectoryForm.$setPristine(); $('#newDirectoryModal').modal('show'); }, submit: function () { $scope.newDirectory.busy = true; $scope.newDirectory.error = null; var filePath = sanitize($scope.cwd + '/' + $scope.newDirectory.name); Client.filesCreateDirectory($scope.appId, filePath, function (error, result) { $scope.newDirectory.busy = false; if (error && error.statusCode === 409) return $scope.newDirectory.error = 'Already exists'; if (error) return Client.error(error); console.log('create direcotory', result); $scope.refresh(); $('#newDirectoryModal').modal('hide'); }); } }; $scope.renameEntry = { busy: false, error: null, entry: null, newName: '', show: function (entry) { $scope.renameEntry.error = null; $scope.renameEntry.entry = entry; $scope.renameEntry.newName = entry.fileName; $scope.renameEntry.busy = false; $('#renameEntryModal').modal('show'); }, submit: function () { $scope.renameEntry.busy = true; var oldFilePath = sanitize($scope.cwd + '/' + $scope.renameEntry.entry.fileName); 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'); }); } }; $scope.entryRemove = { busy: false, error: null, entry: null, show: function (entry) { $scope.entryRemove.error = null; $scope.entryRemove.entry = entry; $('#entryRemoveModal').modal('show'); }, submit: function () { $scope.entryRemove.busy = true; var filePath = sanitize($scope.cwd + '/' + $scope.entryRemove.entry.fileName); 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'); }); } }; 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); } 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); Client.refreshConfig(function (error) { if (error) return Client.initError(error, init); Client.getApp($scope.appId, function (error, result) { if (error) return Client.initError(error, init); $scope.app = result; // now mark the Client to be ready Client.setReady(); $scope.refresh(); $scope.initialized = true; }); }); }); }); } init(); // setup all the dialog focus handling ['newDirectoryModal'].forEach(function (id) { $('#' + id).on('shown.bs.modal', function () { $(this).find("[autofocus]:first").focus(); }); }); }]);