+ events.emit('success', trans('entities.attachments_file_updated'));
+ };
+
+ /**
+ * Delete a file from the server and, on success, the local listing.
+ * @param file
+ */
+ $scope.deleteFile = function(file) {
+ if (!file.deleting) {
+ file.deleting = true;
+ return;
+ }
+ $http.delete(window.baseUrl(`/attachments/${file.id}`)).then(resp => {
+ events.emit('success', resp.data.message);
+ $scope.files.splice($scope.files.indexOf(file), 1);
+ }, checkError('delete'));
+ };
+
+ /**
+ * Attach a link to a page.
+ * @param file
+ */
+ $scope.attachLinkSubmit = function(file) {
+ file.uploaded_to = pageId;
+ $http.post(window.baseUrl('/attachments/link'), file).then(resp => {
+ $scope.files.push(resp.data);
+ events.emit('success', trans('entities.attachments_link_attached'));
+ $scope.file = getCleanFile();
+ }, checkError('link'));
+ };
+
+ /**
+ * Start the edit mode for a file.
+ * @param file
+ */
+ $scope.startEdit = function(file) {
+ $scope.editFile = angular.copy(file);
+ $scope.editFile.link = (file.external) ? file.path : '';
+ };
+
+ /**
+ * Cancel edit mode
+ */
+ $scope.cancelEdit = function() {
+ $scope.editFile = false;
+ };
+
+ /**
+ * Update the name and link of a file.
+ * @param file
+ */
+ $scope.updateFile = function(file) {
+ $http.put(window.baseUrl(`/attachments/${file.id}`), file).then(resp => {
+ let search = filesIndexOf(resp.data);
+ if (search !== -1) $scope.files[search] = resp.data;
+
+ if ($scope.editFile && !file.external) {
+ $scope.editFile.link = '';
+ }
+ $scope.editFile = false;
+ events.emit('success', trans('entities.attachments_updated_success'));
+ }, checkError('edit'));
+ };
+
+ /**
+ * Get the url of a file.
+ */
+ $scope.getFileUrl = function(file) {
+ return window.baseUrl('/attachments/' + file.id);