mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
upload porting first step
This commit is contained in:
@@ -31,7 +31,7 @@ export class AlfrescoContentService {
|
|||||||
* @returns {string} URL address.
|
* @returns {string} URL address.
|
||||||
*/
|
*/
|
||||||
getDocumentThumbnailUrl(document: any): string {
|
getDocumentThumbnailUrl(document: any): string {
|
||||||
return this.authService.alfrescoApi.getDocumentThumbnailUrl(document.entry.id);
|
return this.authService.alfrescoApi.content.getDocumentThumbnailUrl(document.entry.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,6 +40,6 @@ export class AlfrescoContentService {
|
|||||||
* @returns {string} URL address.
|
* @returns {string} URL address.
|
||||||
*/
|
*/
|
||||||
getContentUrl(document: any): string {
|
getContentUrl(document: any): string {
|
||||||
return this.authService.alfrescoApi.getContentUrl(document.entry.id);
|
return this.authService.alfrescoApi.content.getContentUrl(document.entry.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -177,7 +177,7 @@ class DocumentListDemo implements OnInit {
|
|||||||
this.login();
|
this.login();
|
||||||
}
|
}
|
||||||
|
|
||||||
myDocumentActionHandler(obj: any) {
|
myDocumentActionHandler() {
|
||||||
window.alert('my custom action handler');
|
window.alert('my custom action handler');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,7 +20,7 @@
|
|||||||
<script src="node_modules/systemjs/dist/system.src.js"></script>
|
<script src="node_modules/systemjs/dist/system.src.js"></script>
|
||||||
|
|
||||||
<!-- Additional Alfresco libraries -->
|
<!-- Additional Alfresco libraries -->
|
||||||
<script src="node_modules/alfresco-js-api/bundle.js"></script>
|
<script src="node_modules/alfresco-js-api/dist/alfresco-js-api.js"></script>
|
||||||
|
|
||||||
<script src="systemjs.config.js"></script>
|
<script src="systemjs.config.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
@@ -109,6 +109,7 @@ export class MyDemoApp implements OnInit {
|
|||||||
this.authService.login('admin', 'admin').subscribe(
|
this.authService.login('admin', 'admin').subscribe(
|
||||||
token => {
|
token => {
|
||||||
console.log(token);
|
console.log(token);
|
||||||
|
this.token = token;
|
||||||
this.authenticated = true;
|
this.authenticated = true;
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
|
@@ -19,6 +19,7 @@ module.exports = function (config) {
|
|||||||
{pattern: 'node_modules/material-design-lite/material.min.js', included: true, watched: false},
|
{pattern: 'node_modules/material-design-lite/material.min.js', included: true, watched: false},
|
||||||
{pattern: 'node_modules/ng2-alfresco-core/dist/**/*.js', included: false, served: true, watched: false},
|
{pattern: 'node_modules/ng2-alfresco-core/dist/**/*.js', included: false, served: true, watched: false},
|
||||||
{pattern: 'node_modules/ng2-translate/**/*.js', included: false, served: true, watched: false},
|
{pattern: 'node_modules/ng2-translate/**/*.js', included: false, served: true, watched: false},
|
||||||
|
{pattern: 'node_modules/alfresco-js-api/dist/alfresco-js-api.js', included: true, watched: false},
|
||||||
|
|
||||||
{pattern: 'karma-test-shim.js', included: true, watched: true},
|
{pattern: 'karma-test-shim.js', included: true, watched: true},
|
||||||
|
|
||||||
@@ -73,7 +74,7 @@ module.exports = function (config) {
|
|||||||
|
|
||||||
// Coverage reporter generates the coverage
|
// Coverage reporter generates the coverage
|
||||||
reporters: ['mocha', 'coverage', 'coveralls', 'kjhtml'],
|
reporters: ['mocha', 'coverage', 'coveralls', 'kjhtml'],
|
||||||
|
|
||||||
// Source files that you wanna generate coverage for.
|
// Source files that you wanna generate coverage for.
|
||||||
// Do not include tests or libraries (these files will be instrumented by Istanbul)
|
// Do not include tests or libraries (these files will be instrumented by Istanbul)
|
||||||
preprocessors: {
|
preprocessors: {
|
||||||
|
@@ -25,8 +25,7 @@ export class UploadServiceMock extends UploadService {
|
|||||||
filesUpload$: Observable<any>;
|
filesUpload$: Observable<any>;
|
||||||
totalCompleted$: Observable<number>;
|
totalCompleted$: Observable<number>;
|
||||||
|
|
||||||
constructor(settings: AlfrescoSettingsService,
|
constructor(settings: AlfrescoSettingsService, authService: AlfrescoAuthenticationService) {
|
||||||
authService: AlfrescoAuthenticationService) {
|
|
||||||
super(settings, authService);
|
super(settings, authService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -82,7 +82,6 @@ export class UploadButtonComponent {
|
|||||||
constructor(public el: ElementRef,
|
constructor(public el: ElementRef,
|
||||||
private _uploaderService: UploadService,
|
private _uploaderService: UploadService,
|
||||||
translate: AlfrescoTranslationService) {
|
translate: AlfrescoTranslationService) {
|
||||||
console.log('UploadComponent constructor', el);
|
|
||||||
|
|
||||||
let formFields = this.createFormFields();
|
let formFields = this.createFormFields();
|
||||||
this._uploaderService.setOptions(formFields);
|
this._uploaderService.setOptions(formFields);
|
||||||
|
@@ -31,8 +31,7 @@ let errorFn = jasmine.createSpy('error');
|
|||||||
|
|
||||||
class MockUploadService extends UploadService {
|
class MockUploadService extends UploadService {
|
||||||
|
|
||||||
constructor(settings: AlfrescoSettingsService,
|
constructor(settings: AlfrescoSettingsService, authService: AlfrescoAuthenticationService) {
|
||||||
authService: AlfrescoAuthenticationService) {
|
|
||||||
super(settings, authService);
|
super(settings, authService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,7 +20,7 @@ import { EventEmitter, Injectable } from '@angular/core';
|
|||||||
import { Response } from '@angular/http';
|
import { Response } from '@angular/http';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
import { Observer } from 'rxjs/Observer';
|
import { Observer } from 'rxjs/Observer';
|
||||||
import { AlfrescoSettingsService, AlfrescoAuthenticationService } from 'ng2-alfresco-core';
|
import { AlfrescoSettingsService, AlfrescoAuthenticationService} from 'ng2-alfresco-core';
|
||||||
import { FileModel } from '../models/file.model';
|
import { FileModel } from '../models/file.model';
|
||||||
|
|
||||||
declare let AlfrescoApi: any;
|
declare let AlfrescoApi: any;
|
||||||
@@ -35,8 +35,6 @@ declare let AlfrescoApi: any;
|
|||||||
export class UploadService {
|
export class UploadService {
|
||||||
private _url: string = '/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children';
|
private _url: string = '/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children';
|
||||||
|
|
||||||
private _method: string = 'POST';
|
|
||||||
private _fieldName: string = 'filedata';
|
|
||||||
private _formFields: Object = {};
|
private _formFields: Object = {};
|
||||||
|
|
||||||
private _queue: FileModel[] = [];
|
private _queue: FileModel[] = [];
|
||||||
@@ -50,12 +48,10 @@ export class UploadService {
|
|||||||
|
|
||||||
public totalCompleted: number = 0;
|
public totalCompleted: number = 0;
|
||||||
|
|
||||||
constructor(private settings: AlfrescoSettingsService,
|
constructor(private settings: AlfrescoSettingsService, private authService: AlfrescoAuthenticationService) {
|
||||||
private authService: AlfrescoAuthenticationService) {
|
|
||||||
console.log('UploadService constructor');
|
|
||||||
this.filesUpload$ = new Observable<FileModel[]>(observer => this._filesUploadObserver = observer).share();
|
this.filesUpload$ = new Observable<FileModel[]>(observer => this._filesUploadObserver = observer).share();
|
||||||
this.totalCompleted$ = new Observable<number>(observer => this._totalCompletedObserver = observer).share();
|
this.totalCompleted$ = new Observable<number>(observer => this._totalCompletedObserver = observer).share();
|
||||||
this._alfrescoClient = this.getAlfrescoClient();
|
this._alfrescoClient = this.authService.alfrescoApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,14 +89,6 @@ export class UploadService {
|
|||||||
return this._formFields;
|
return this._formFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the alfresco client
|
|
||||||
* @returns {AlfrescoApi.ApiClient}
|
|
||||||
*/
|
|
||||||
private getAlfrescoClient() {
|
|
||||||
return AlfrescoApi.getClientWithTicket(this.settings.getApiBaseUrl(), this.authService.getToken());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add files to the uploading queue to be uploaded.
|
* Add files to the uploading queue to be uploaded.
|
||||||
*
|
*
|
||||||
@@ -133,87 +121,35 @@ export class UploadService {
|
|||||||
});
|
});
|
||||||
filesToUpload.forEach((uploadingFileModel) => {
|
filesToUpload.forEach((uploadingFileModel) => {
|
||||||
uploadingFileModel.setUploading();
|
uploadingFileModel.setUploading();
|
||||||
this.uploadFile(uploadingFileModel, directory, elementEmit);
|
this.authService.alfrescoApi.
|
||||||
});
|
upload.uploadFile(uploadingFileModel.file, directory)
|
||||||
}
|
.on('progress', (progress: any) => {
|
||||||
|
uploadingFileModel.setProgres(progress);
|
||||||
|
})
|
||||||
|
.on('abort', () => {
|
||||||
|
uploadingFileModel.setAbort();
|
||||||
|
})
|
||||||
|
.on('error', () => {
|
||||||
|
uploadingFileModel.setError();
|
||||||
|
})
|
||||||
|
.on('success', (data: any) => {
|
||||||
|
elementEmit.emit({
|
||||||
|
value: 'File uploaded'
|
||||||
|
});
|
||||||
|
uploadingFileModel.onFinished(
|
||||||
|
data.status,
|
||||||
|
data.statusText,
|
||||||
|
data.response
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* Create an XMLHttpRequest and return it
|
|
||||||
* @returns {XMLHttpRequest}
|
|
||||||
*/
|
|
||||||
createXMLHttpRequestInstance(uploadingFileModel: any, elementEmit: EventEmitter<any>) {
|
|
||||||
let xmlHttpRequest = new XMLHttpRequest();
|
|
||||||
xmlHttpRequest.upload.onprogress = (e) => {
|
|
||||||
if (e.lengthComputable) {
|
|
||||||
let percent = Math.round(e.loaded / e.total * 100);
|
|
||||||
uploadingFileModel.setProgres({
|
|
||||||
total: e.total,
|
|
||||||
loaded: e.loaded,
|
|
||||||
percent: percent
|
|
||||||
});
|
|
||||||
if (this._filesUploadObserver) {
|
|
||||||
this._filesUploadObserver.next(this._queue);
|
this._filesUploadObserver.next(this._queue);
|
||||||
}
|
if (!uploadingFileModel.abort && !uploadingFileModel.error) {
|
||||||
}
|
if (this._totalCompletedObserver) {
|
||||||
};
|
this._totalCompletedObserver.next(++this.totalCompleted);
|
||||||
|
}
|
||||||
xmlHttpRequest.upload.onabort = (e) => {
|
|
||||||
uploadingFileModel.setAbort();
|
|
||||||
};
|
|
||||||
|
|
||||||
xmlHttpRequest.upload.onerror = (e) => {
|
|
||||||
uploadingFileModel.setError();
|
|
||||||
};
|
|
||||||
|
|
||||||
xmlHttpRequest.onreadystatechange = () => {
|
|
||||||
if (xmlHttpRequest.readyState === XMLHttpRequest.DONE) {
|
|
||||||
elementEmit.emit({
|
|
||||||
value: 'File uploaded'
|
|
||||||
});
|
|
||||||
uploadingFileModel.onFinished(
|
|
||||||
xmlHttpRequest.status,
|
|
||||||
xmlHttpRequest.statusText,
|
|
||||||
xmlHttpRequest.response
|
|
||||||
);
|
|
||||||
this._filesUploadObserver.next(this._queue);
|
|
||||||
if (!uploadingFileModel.abort && !uploadingFileModel.error) {
|
|
||||||
if (this._totalCompletedObserver) {
|
|
||||||
this._totalCompletedObserver.next(++this.totalCompleted);
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
|
||||||
};
|
|
||||||
return xmlHttpRequest;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Upload a file into the directory folder, and enrich it with the xhr.
|
|
||||||
*
|
|
||||||
* @param {FileModel} - files to be uploaded.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
uploadFile(uploadingFileModel: FileModel, directory: string, elementEmit: EventEmitter<any>): void {
|
|
||||||
// Configure HTTP basic authorization: basicAuth
|
|
||||||
let basicAuth = this._alfrescoClient.authentications['basicAuth'];
|
|
||||||
|
|
||||||
let form = new FormData();
|
|
||||||
form.append(this._fieldName, uploadingFileModel.file, uploadingFileModel.name);
|
|
||||||
Object.keys(this._formFields).forEach((key: any) => {
|
|
||||||
form.append(key, this._formFields[key]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
form.append('relativePath', directory);
|
|
||||||
|
|
||||||
let xmlHttpRequest = this.createXMLHttpRequestInstance(uploadingFileModel, elementEmit);
|
|
||||||
uploadingFileModel._xmlHttpRequest = xmlHttpRequest;
|
|
||||||
|
|
||||||
xmlHttpRequest.open(this._method, this.getHost() + this.getUrl(), true);
|
|
||||||
let authToken = btoa(basicAuth.username + ':' + basicAuth.password);
|
|
||||||
if (authToken) {
|
|
||||||
xmlHttpRequest.setRequestHeader('Authorization', `${basicAuth.type} ${authToken}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
xmlHttpRequest.send(form);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user