lint login and upload

This commit is contained in:
Mario Romano
2016-05-12 19:35:41 +01:00
parent 6654563469
commit 6b99e49498
22 changed files with 642 additions and 672 deletions

View File

@@ -34,63 +34,63 @@ declare let __moduleName: string;
* @returns {UploadDragAreaComponent} .
*/
@Component({
selector: 'alfresco-upload-drag-area',
moduleId: __moduleName,
directives: [FileDraggableDirective, FileUploadingDialogComponent],
templateUrl: './upload-drag-area.component.html',
styleUrls: ['./upload-drag-area.component.css']
selector: 'alfresco-upload-drag-area',
moduleId: __moduleName,
directives: [FileDraggableDirective, FileUploadingDialogComponent],
templateUrl: './upload-drag-area.component.html',
styleUrls: ['./upload-drag-area.component.css']
})
export class UploadDragAreaComponent {
private _uploaderService: UploadService;
private _uploaderService: UploadService;
@ViewChild('fileUploadingDialog')
fileUploadingDialogComponent: FileUploadingDialogComponent;
@ViewChild('fileUploadingDialog')
fileUploadingDialogComponent: FileUploadingDialogComponent;
@Input()
showUploadDialog: boolean = true;
@Input()
showUploadDialog: boolean = true;
filesUploadingList: FileModel [] = [];
filesUploadingList: FileModel [] = [];
@Input()
uploaddirectory: string = '';
@Input()
uploaddirectory: string = '';
constructor(public el: ElementRef) {
console.log('UploadComponent constructor', el);
constructor(public el: ElementRef) {
console.log('UploadComponent constructor', el);
this._uploaderService = new UploadService({
url: 'http://192.168.99.100:8080/alfresco/service/api/upload',
withCredentials: true,
authToken: btoa('admin:admin'),
authTokenPrefix: 'Basic',
fieldName: 'filedata',
formFields: {
siteid: 'swsdp',
containerid: 'documentLibrary'
}
});
}
/**
* Method called when files are dropped in the drag area.
*
* @param {File[]} files - files dropped in the drag area.
*/
onFilesDropped(files: File[]): void {
if (files.length) {
this._uploaderService.addToQueue(files);
this._uploaderService.uploadFilesInTheQueue(this.uploaddirectory);
this.filesUploadingList = this._uploaderService.getQueue();
if (this.showUploadDialog) {
this._showDialog();
}
this._uploaderService = new UploadService({
url: 'http://192.168.99.100:8080/alfresco/service/api/upload',
withCredentials: true,
authToken: btoa('admin:admin'),
authTokenPrefix: 'Basic',
fieldName: 'filedata',
formFields: {
siteid: 'swsdp',
containerid: 'documentLibrary'
}
});
}
}
/**
* Show the upload dialog.
*/
private _showDialog(): void {
this.fileUploadingDialogComponent.showDialog();
}
/**
* Method called when files are dropped in the drag area.
*
* @param {File[]} files - files dropped in the drag area.
*/
onFilesDropped(files: File[]): void {
if (files.length) {
this._uploaderService.addToQueue(files);
this._uploaderService.uploadFilesInTheQueue(this.uploaddirectory);
this.filesUploadingList = this._uploaderService.getQueue();
if (this.showUploadDialog) {
this._showDialog();
}
}
}
/**
* Show the upload dialog.
*/
private _showDialog(): void {
this.fileUploadingDialogComponent.showDialog();
}
}

View File

@@ -1,76 +0,0 @@
/**
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { FileModel } from '../models/file.model';
/**
*
* UploadService keep the queue of the file to upload and uploads them.
*
* @returns {UploadService} .
*/
export declare class UploadService {
private options;
private _url;
private _method;
private _authTokenPrefix;
private _authToken;
private _fieldName;
private _formFields;
private _withCredentials;
private _xmlHttpRequest;
private _queue;
constructor(options: any);
/**
* Add files to the uploading queue to be uploaded.
*
* @param {File[]} - files to add to the upload queue.
*
* return {FileModel[]} - return the file added to the queue in this call.
*/
addToQueue(files: any[]): FileModel[];
/**
* Pick all the files in the queue that are not been uploaded yet and upload it into the directory folder.
*/
uploadFilesInTheQueue(directory: string): void;
/**
* The method create a new XMLHttpRequest instance if doesn't exist
*/
private _configureXMLHttpRequest(uploadingFileModel);
/**
* Upload a file into the directory folder, and enrich it with the xhr.
*
* @param {FileModel} - files to be uploaded.
*
*/
uploadFile(uploadingFileModel: any, directory: string): void;
/**
* Return all the files in the uploading queue.
*
* @return {FileModel[]} - files in the upload queue.
*/
getQueue(): FileModel[];
/**
* Check if an item is a file.
*
* @return {boolean}
*/
private _isFile(file);
/**
* Set XMLHttpRequest method
* @param xhr
*/
setXMLHttpRequest(xhr: XMLHttpRequest): void;
}

View File

@@ -1,166 +0,0 @@
/**
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
System.register(['../models/file.model'], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var file_model_1;
var UploadService;
return {
setters:[
function (file_model_1_1) {
file_model_1 = file_model_1_1;
}],
execute: function() {
/**
*
* UploadService keep the queue of the file to upload and uploads them.
*
* @returns {UploadService} .
*/
UploadService = (function () {
function UploadService(options) {
this.options = options;
this._method = 'POST';
this._authTokenPrefix = 'Basic';
this._authToken = undefined;
this._fieldName = 'file';
this._formFields = {};
this._queue = [];
console.log('UploadService constructor');
this._withCredentials = options.withCredentials != null ? options.withCredentials : this._withCredentials;
this._url = options.url != null ? options.url : this._url;
this._authTokenPrefix = options.authTokenPrefix != null ? options.authTokenPrefix : this._authTokenPrefix;
this._authToken = options.authToken != null ? options.authToken : this._authToken;
this._fieldName = options.fieldName != null ? options.fieldName : this._fieldName;
this._formFields = options.formFields != null ? options.formFields : this._formFields;
}
/**
* Add files to the uploading queue to be uploaded.
*
* @param {File[]} - files to add to the upload queue.
*
* return {FileModel[]} - return the file added to the queue in this call.
*/
UploadService.prototype.addToQueue = function (files) {
var latestFilesAdded = [];
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
var file = files_1[_i];
if (this._isFile(file)) {
var uploadingFileModel = new file_model_1.FileModel(file);
latestFilesAdded.push(uploadingFileModel);
this._queue.push(uploadingFileModel);
}
}
return latestFilesAdded;
};
/**
* Pick all the files in the queue that are not been uploaded yet and upload it into the directory folder.
*/
UploadService.prototype.uploadFilesInTheQueue = function (directory) {
var _this = this;
var filesToUpload = this._queue.filter(function (uploadingFileModel) {
return !uploadingFileModel.uploading && !uploadingFileModel.done && !uploadingFileModel.abort && !uploadingFileModel.error;
});
filesToUpload.forEach(function (uploadingFileModel) {
uploadingFileModel.setUploading();
_this.uploadFile(uploadingFileModel, directory);
});
};
;
/**
* The method create a new XMLHttpRequest instance if doesn't exist
*/
UploadService.prototype._configureXMLHttpRequest = function (uploadingFileModel) {
var _this = this;
if (this._xmlHttpRequest == undefined) {
this._xmlHttpRequest = new XMLHttpRequest();
this._xmlHttpRequest.upload.onprogress = function (e) {
if (e.lengthComputable) {
var percent = Math.round(e.loaded / e.total * 100);
uploadingFileModel.setProgres({
total: e.total,
loaded: e.loaded,
percent: percent
});
}
};
this._xmlHttpRequest.upload.onabort = function (e) {
uploadingFileModel.setAbort();
};
this._xmlHttpRequest.upload.onerror = function (e) {
uploadingFileModel.setError();
};
this._xmlHttpRequest.onreadystatechange = function () {
if (_this._xmlHttpRequest.readyState === XMLHttpRequest.DONE) {
uploadingFileModel.onFinished(_this._xmlHttpRequest.status, _this._xmlHttpRequest.statusText, _this._xmlHttpRequest.response);
}
};
}
};
/**
* Upload a file into the directory folder, and enrich it with the xhr.
*
* @param {FileModel} - files to be uploaded.
*
*/
UploadService.prototype.uploadFile = function (uploadingFileModel, directory) {
var _this = this;
var form = new FormData();
form.append(this._fieldName, uploadingFileModel.file, uploadingFileModel.name);
Object.keys(this._formFields).forEach(function (key) {
form.append(key, _this._formFields[key]);
});
form.append('uploaddirectory', directory);
this._configureXMLHttpRequest(uploadingFileModel);
uploadingFileModel.setXMLHttpRequest(this._xmlHttpRequest);
this._xmlHttpRequest.open(this._method, this._url, true);
this._xmlHttpRequest.withCredentials = this._withCredentials;
if (this._authToken) {
this._xmlHttpRequest.setRequestHeader('Authorization', this._authTokenPrefix + " " + this._authToken);
}
this._xmlHttpRequest.send(form);
};
/**
* Return all the files in the uploading queue.
*
* @return {FileModel[]} - files in the upload queue.
*/
UploadService.prototype.getQueue = function () {
return this._queue;
};
/**
* Check if an item is a file.
*
* @return {boolean}
*/
UploadService.prototype._isFile = function (file) {
return file !== null && (file instanceof Blob || (file.name && file.size));
};
/**
* Set XMLHttpRequest method
* @param xhr
*/
UploadService.prototype.setXMLHttpRequest = function (xhr) {
this._xmlHttpRequest = xhr;
};
return UploadService;
}());
exports_1("UploadService", UploadService);
}
}
});
//# sourceMappingURL=upload.service.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"upload.service.js","sourceRoot":"","sources":["upload.service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;;;;;;;;;;;;YAMH;;;;;eAKG;YACH;gBAYI,uBAAoB,OAAY;oBAAZ,YAAO,GAAP,OAAO,CAAK;oBAVxB,YAAO,GAAW,MAAM,CAAC;oBACzB,qBAAgB,GAAW,OAAO,CAAC;oBACnC,eAAU,GAAW,SAAS,CAAC;oBAC/B,eAAU,GAAW,MAAM,CAAC;oBAC5B,gBAAW,GAAW,EAAE,CAAC;oBAIzB,WAAM,GAAgB,EAAE,CAAC;oBAG7B,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;oBAEzC,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,eAAe,IAAI,IAAI,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,gBAAgB,CAAC;oBAC1G,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,GAAG,IAAI,IAAI,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC;oBAC1D,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,eAAe,IAAI,IAAI,GAAG,OAAO,CAAC,eAAe,GAAG,IA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AC,IAAI,CAAC,CAAC;gBACpC,CAAC;gBAED;;;;mBAIG;gBACH,gCAAQ,GAAR;oBACI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;gBACvB,CAAC;gBAED;;;;mBAIG;gBACK,+BAAO,GAAf,UAAgB,IAAS;oBACrB,MAAM,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,IAAI,YAAY,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;gBAC/E,CAAC;gBAED;;;mBAGG;gBACI,yCAAiB,GAAxB,UAAyB,GAAmB;oBACxC,IAAI,CAAC,eAAe,GAAG,GAAG,CAAC;gBAC/B,CAAC;gBACL,oBAAC;YAAD,CAAC,AAlJD,IAkJC;YAlJD,yCAkJC,CAAA"}

View File

@@ -124,7 +124,7 @@ export class UploadService {
* @param {FileModel} - files to be uploaded.
*
*/
uploadFile(uploadingFileModel: any, directory: string): void {
uploadFile(uploadingFileModel: FileModel, directory?: string): void {
let form = new FormData();
form.append(this._fieldName, uploadingFileModel.file, uploadingFileModel.name);
Object.keys(this._formFields).forEach((key: any) => {