Move uploading logic to dedicated FileService

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-06-13 18:57:26 +02:00
parent 7b230d567e
commit 8229fbacea
6 changed files with 134 additions and 55 deletions

View File

@@ -22,7 +22,7 @@
import app from '../app/App.js';
/* global oc_defaults OC */
app.controller('BoardController', function ($rootScope, $scope, $stateParams, StatusService, BoardService, StackService, CardService, LabelService, $state, $transitions, $filter) {
app.controller('BoardController', function ($rootScope, $scope, $stateParams, StatusService, BoardService, StackService, CardService, LabelService, $state, $transitions, $filter, FileService) {
$scope.sidebar = $rootScope.sidebar;
@@ -40,6 +40,7 @@ app.controller('BoardController', function ($rootScope, $scope, $stateParams, St
$scope.labelservice = LabelService;
$scope.defaultColors = ['31CC7C', '317CCC', 'FF7A66', 'F1DB50', '7C31CC', 'CC317C', '3A3B3D', 'CACBCD'];
$scope.board = BoardService.getCurrent();
$scope.uploader = FileService.uploader;
// workaround for $stateParams changes not being propagated
$scope.$watch(function() {
@@ -353,11 +354,11 @@ app.controller('BoardController', function ($rootScope, $scope, $stateParams, St
};
};
$scope.attachmentCount = function(attachments) {
if (Array.isArray(attachments)) {
return attachments.filter((obj) => obj.deletedAt === 0).length;
$scope.attachmentCount = function(card) {
if (Array.isArray(card.attachments)) {
return card.attachments.filter((obj) => obj.deletedAt === 0).length;
}
return attachments;
return card.attachmentCount;
};
});

View File

@@ -23,7 +23,7 @@
/* global app moment */
import app from '../app/App.js';
app.controller('CardController', function ($scope, $rootScope, $sce, $location, $stateParams, $interval, $timeout, $filter, BoardService, CardService, StackService, StatusService, markdownItConverter, FileUploader) {
app.controller('CardController', function ($scope, $rootScope, $sce, $location, $stateParams, $interval, $timeout, $filter, BoardService, CardService, StackService, StatusService, markdownItConverter, FileService) {
$scope.sidebar = $rootScope.sidebar;
$scope.status = {
lastEdit: 0,
@@ -31,49 +31,13 @@ app.controller('CardController', function ($scope, $rootScope, $sce, $location,
};
$scope.cardservice = CardService;
$scope.fileservice = FileService;
$scope.cardId = $stateParams.cardId;
$scope.statusservice = StatusService.getInstance();
$scope.boardservice = BoardService;
$scope.uploader = new FileUploader();
$scope.runUpload = function(fileItem) {
fileItem.url = OC.generateUrl('/apps/deck/cards/' + $scope.cardId + '/attachment');
fileItem.formData = [
{
requesttoken: oc_requesttoken,
type: 'deck_file',
}
];
$scope.uploader.uploadItem(fileItem);
};
$scope.uploader.onAfterAddingFile = function(fileItem) {
let existingFile = $scope.cardservice.getCurrent().attachments.find((attachment) => {
return attachment.data === fileItem.file.name;
});
if (typeof existingFile !== 'undefined') {
OC.dialogs.confirm(
`A file with the name ${fileItem.file.name} already exists. Do you want to overwrite it?`,
'File already exists',
function(result) {
if (result) {
$scope.runUpload(fileItem);
} else {
// TODO: check for proper number and append it before the file extension
// TODO: iterate over attachments (check if matches "file.name (n)") increase n
fileItem.file.name = fileItem.file.name + '.1';
}
}
);
} else {
$scope.runUpload(fileItem);
}
};
$scope.uploader.onSuccessItem = function(item, response) {
$scope.cardservice.getCurrent().attachments.push(response);
};
$scope.isArray = angular.isArray;
$scope.mimetypeForAttachment = function(attachment) {
let url = OC.MimeType.getIconUrl(attachment.extendedData.mimetype);