(function () { "use strict"; angular .module("agora.plus.formComponent") .directive("uploadFormComp", uploadFormComp); uploadFormComp.$inject = ["EventStoreService", "$http", "$translate", "Flash", "$timeout"]; function uploadFormComp(EventStoreService, $http, $translate, Flash, $timeout) { return { restrict: "E", require: "ngModel", link: linking, scope: { identifier: "@", apIconName: "@", replaceLabel: "@", fileName: "@?", types: "=?", maxSize: "=?", apLabel: "= 2) { scope.ctrl.fileIsTooBigCount++; } } if (scope.ctrl.initialLength == 1 && !scope.ctrl.apMultiple) { scope.ctrl.singleFileUpload = false; } scope.ctrl.setFileIsTooBigCountMessage(scope.ctrl.fileIsTooBigCount); return; } else { if (scope.ctrl.fileIsTooBigCount != 0) { scope.ctrl.fileIsTooBigCount = 0; } file.fileIsTooBig = false; } //if (scope.ctrl.checkMimeType(file.type)) { if (true) { if (file.fileUploaded) { return; } file.fileUploaded = true; file.messageToDisplay = $translate.instant("UPLOADING"); file.fileIsUploading = true; // var formData = new FormData(); // formData.append("myFile", file); scope.ctrl.finalFileType = ""; /* if (file.type == "") { scope.ctrl.tempExtension = file.name.split('.').pop(); if (scope.ctrl.tempExtension == "doc") { scope.ctrl.finalFileType = "application/msword"; } else if (scope.ctrl.tempExtension == "docx") { scope.ctrl.finalFileType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; } } else { scope.ctrl.finalFileType = file.type; } */ scope.ctrl.finalFileType = file.type; //header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, X-File-Name, X-File-Size, X-File-Type"); if (!file.fileIsTooBig) { /* var ola = { "size": String(file.size), "mimeType": String(scope.ctrl.finalFileType), "name": String(file.name), "content": formData.serializeArray() }; */ var formData = new FormData(); formData.append("myFile", file); //formData.append("name", String(file.name)); //formData.append("mimeType", String(scope.ctrl.finalFileType)); //formData.append("size", String(file.size)); $http.post(scope.ctrl.api +"?name=" +String(file.name)+"&mimeType="+String(scope.ctrl.finalFileType)+"&size="+String(file.size) , file, { transformRequest: angular.identity, headers: { "Content-Type": undefined, } }).then(function (data) { if (data.status == 200) { if (data.data.TYPE_RESULT == "1") { file.fileIsUploading = false; file.fileFinishedUpload = true; file.iconToShow = "good"; file.messageToDisplay = $translate.instant("UPLOAD_SUCESS"); file.guid = data.data.DATA.NAME_FILE; file.tmpFilePath = data.data.DATA.TMP_FILE_PATH; if ((scope.ctrl.apIsKidComponent && scope.ctrl.apIsKidComponent === true) || (scope.ctrl.apGetPreviewUrl && scope.ctrl.apGetPreviewUrl === true)) { var url = window.URL || window.webkitURL; var downloadUrl = url.createObjectURL((scope.ctrl.inputElement.files[file.idx])); } if (scope.ctrl.singleFileUpload) { scope.ctrl.files = { name: file.name, guid: file.guid, mimeType: file.type, tempUrl: downloadUrl ? downloadUrl : '', tmpUrl: file.tmpFilePath }; } else { if (!scope.ctrl.files) { scope.ctrl.files = []; } scope.ctrl.files.push({ name: file.name, guid: file.guid, mimeType: file.type, tempUrl: downloadUrl ? downloadUrl : '', tmpUrl: file.tmpFilePath }); } ngModelController.$setViewValue(scope.ctrl.files); for (var z = 0; z < scope.ctrl.inputElement.files.length; z++) { /* if (scope.ctrl.inputElement.files[z].type == "" || scope.ctrl.inputElement.files[z].type == "application/msword" || scope.ctrl.inputElement.files[z].type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { scope.ctrl.filePDF.push({ file: scope.ctrl.inputElement.files[z], type: "doc", name: scope.ctrl.inputElement.files[z].name }); } else { scope.ctrl.filePDF.push(new Blob([scope.ctrl.inputElement.files[z]], { type: scope.ctrl.inputElement.files[z].type, name: scope.ctrl.inputElement.files[z].name })); } */ scope.ctrl.filePDF.push(new Blob([scope.ctrl.inputElement.files[z]], { type: scope.ctrl.inputElement.files[z].type, name: scope.ctrl.inputElement.files[z].name })); } } else { file.hadError = true; file.uploadFailed = true; file.fileIsUploading = false; file.iconToShow = "error"; for (var i = 0; i < scope.ctrl.fileList.length; i++) { scope.ctrl.fileList[i]["hasError"] = 1 } ngModelController.$setViewValue(scope.ctrl.fileList); file.messageToDisplay = $translate.instant("ERROR_FILE_UPLOAD"); } scope.ctrl.dragOver = false; } EventStoreService.setEventStore({ eventType: "fileUpload_" + scope.identifier, func: function () { scope.ctrl.fileName = data.data.DATA.NAME_FILE; return scope.ctrl.fileName; } }); }).catch(function () { file.hadError = true; file.uploadFailed = true; file.fileIsUploading = false; file.iconToShow = "error"; file.messageToDisplay = $translate.instant("ERROR_FILE_UPLOAD"); for (var i = 0; i < scope.ctrl.fileList.length; i++) { scope.ctrl.fileList[i]["hasError"] = 1 } ngModelController.$setViewValue(scope.ctrl.fileList); console.error("Error uploading file"); }).finally(function () { scope.ctrl.setFileIsTooBigCountMessage(scope.ctrl.fileIsTooBigCount); }); } } else { file.hadError = true; // Flash.create("danger", $translate.instant("ERROR_INVALID_FILE_TYPE")); file.messageToDisplay = $translate.instant("ERROR_INVALID_FILE_TYPE"); } }); for (var i = 0; i < scope.ctrl.fileList.length; i++) { if (scope.ctrl.fileList[i].fileIsTooBig) { scope.ctrl.fileList.splice(i, 1); i--; } } } function overDropzone(event) { // event.preventDefault(); if (!scope.ctrl.dragOver) { scope.ctrl.dragOver = true; scope.$apply(); } } function leftDropzone(event) { if (scope.ctrl.dragOver) { scope.ctrl.dragOver = false; scope.$apply(); } } function fileDropped(event) { if (scope.ctrl.dragOver) { scope.ctrl.dragOver = false; scope.$apply(); } } } uploadFormController.$inject = ["$scope", "$document", "constant"]; function uploadFormController($scope, $document, constant) { var vm = this; vm.imgPath = constant.imgPath; vm.fileUploaded = false; vm.fileIsUploading = false; vm.successfulUpload = false; vm.uploadFailed = false; vm.hadError = false; vm.dragOver = false; vm.viewUploadedFile = viewUploadedFile; vm.checkMimeType = checkMimeType; vm.deleteFile = deleteFile; vm.retryUpload = retryUpload; vm.deleteFileAfterSuccess = deleteFileAfterSuccess; vm.showInfo = showInfo; vm.setFileIsTooBigCountMessage = setFileIsTooBigCountMessage; vm.insertFileFromDraftIntoFileList = insertFileFromDraftIntoFileList; vm.iconToShow = "error"; vm.guids = []; vm.filePDF = []; vm.wordDoc = false; vm.tempExtension = ""; vm.tempFileList = []; vm.finalFileType = ""; vm.errorFileType= false; vm.blueActive = false; vm.showDetails = false; function viewUploadedFile(index) { if (vm.filePDF[index].type != "application/pdf" && vm.filePDF[index].type != "image/jpeg" && vm.filePDF[index].type != "image/png" && vm.filePDF[index].type != "image/svg+xml") { var a = document.createElement("a"); a.style.display = "none"; var url = window.URL || window.webkitURL; var downloadUrl = url.createObjectURL(vm.fileList[index]); a.href = downloadUrl; a.download = vm.fileList[index].name; document.body.appendChild(a); a.click(); } else { if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(vm.filePDF[index], vm.fileList[index].name); } else { var fileURL = URL.createObjectURL(vm.filePDF[index]); window.open(fileURL); } } } function showInfo() { if (vm.showDetails == false) { vm.showDetails = true; vm.blueActive = true; } else { vm.showDetails = false; vm.blueActive = false; } } function deleteFileAfterSuccess(guid, index) { //vm.ngModelController.$setViewValue(null); $http.post(PCK_GLOBAL_VARIABLES.webservice_path_API_DASHBOARD + "/delete/file/?nameFile=" + guid, {headers: {"Content-Type": "application/json"}} ).then(function (data) { if (data.status != 200 || data.data.TYPE_RESULT != "1") { console.log([data.data.ERROR.ID, ": ", data.data.ERROR.MESSAGE].join("")); } }).catch(function (error) { console.log(error); }); if (Array.isArray(vm.files) === false){ vm.files = {}; } else { vm.files.splice(index,1); } vm.ngModelController.$setViewValue(null); vm.ngModelController.$setViewValue(vm.files); vm.filePDF.splice(index, 1); vm.fileList.splice(index, 1); vm.inputElement.value = ""; } function deleteFile(index) { $http.delete(vm.api + "/?nameFile=" + vm.fileList[index].guid) .then(function (data) { if (data.status != 200 || data.data.TYPE_RESULT != "1") { console.log([data.data.ERROR.ID, ": ", data.data.ERROR.MESSAGE].join("")); } vm.fileList.forEach(function (file, index, array) { file.idx = index; }); }).catch(function (error) { console.log(error); }); vm.fileList.splice(index, 1); vm.inputElement.value = ""; if (vm.singleFileUpload) { vm.singleFileUpload = false; } } function checkMimeType(type) { if (vm.types == type) { return true; } else { for (var i = 0; i < vm.types.length; i++) { //Fallback method when the mime type is not identified, first checks if in the input .doc files are allowed if (vm.types[i] == "application/msword" || vm.types[i] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { return true; } if (type == vm.types[i]) { return true; } } return false; } } function retryUpload(file) { file.uploadFailed = false; file.hadError = false; file.fileIsUploading = true; file.fileUploaded = false; vm.uploadFunc(); } function setFileIsTooBigCountMessage(count) { if (count == 1) { vm.fileIsTooBigMessage = $translate.instant("ERROR_FILE_TOO_BIG"); } else if (count > 1) { vm.fileIsTooBigMessage = $translate.instant("ERROR_FILES_TOO_BIG"); } } function insertFileFromDraftIntoFileList(file) { delete file.fromDraft; if (Array.isArray(file)) { vm.fileList = []; if (file.length > 1) { vm.singleFileUpload = false; } else if (Array.isArray(file[0])) { vm.singleFileUpload = false; } else { vm.singleFileUpload = true; } for (var i = 0; i < file.length; i++) { file[i].fileFinishedUpload = true; file[i].fileIsTooBig = false; file[i].fileIsUploading = false; file[i].fileUploaded = true; file[i].iconToShow = "good"; file[i].guid = file[i].guid; file[i].messageToDisplay = $translate.instant("UPLOAD_SUCESS"); vm.fileList.push(file[i]); } vm.files = []; for (var i = 0; i < vm.fileList.length; i++) { vm.files.push(vm.fileList[i]); } vm.ngModelController.$setViewValue(vm.files); } else { vm.singleFileUpload = true; file.fileFinishedUpload = true; file.fileIsTooBig = false; file.fileIsUploading = false; file.fileUploaded = true; file.iconToShow = "good"; file.guid = file.guid; file.messageToDisplay = $translate.instant("UPLOAD_SUCESS"); vm.fileList = []; vm.fileList.push(file); } } } } }).call();