+ /**
+ * Used by dropzone to get the endpoint to upload to.
+ * @returns {string}
+ */
+ $scope.getUploadUrl = function (file) {
+ let suffix = (typeof file !== 'undefined') ? `/${file.id}` : '';
+ return window.baseUrl(`/files/upload${suffix}`);
+ };
+
+ /**
+ * Get files for the current page from the server.
+ */
+ function getFiles() {
+ let url = window.baseUrl(`/files/get/page/${pageId}`)
+ $http.get(url).then(resp => {
+ $scope.files = resp.data;
+ currentOrder = resp.data.map(file => {return file.id}).join(':');
+ }, checkError('get'));
+ }
+ getFiles();
+
+ /**
+ * Runs on file upload, Adds an file to local file list
+ * and shows a success message to the user.
+ * @param file
+ * @param data
+ */
+ $scope.uploadSuccess = function (file, data) {
+ $scope.$apply(() => {
+ $scope.files.push(data);
+ });
+ events.emit('success', 'File uploaded');
+ };
+
+ /**
+ * Upload and overwrite an existing file.
+ * @param file
+ * @param data
+ */
+ $scope.uploadSuccessUpdate = function (file, data) {
+ $scope.$apply(() => {
+ let search = filesIndexOf(data);
+ if (search !== -1) $scope.files[search] = data;