All files / src Upload.ts

9.68% Statements 3/31
0% Branches 0/10
0% Functions 0/7
10% Lines 3/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65    1x 1x   1x                                                                                                                      
 
import { Content } from './Content';
import { Subject, Observable } from '@reactivex/rxjs';
import { SavedContent, ODataHelper, Authentication } from './SN';
 
export class Upload {
 
    public static Text<T extends Content>(textContent: string, content: T, PropertyName: keyof T | 'Binary' = 'Binary'){
 
    }
 
    public static File<T extends Content>(scope: SavedContent<Content>, File: File, ContentType: { new(...args): T } = File as any, PropertyName: keyof T | 'Binary' = 'Binary', Settings: {
        Overwrite: boolean,
        UseChunk: boolean
    } = {
            Overwrite: false,
            UseChunk: false
        }): Observable<SavedContent<T>> {
 
        return scope.GetRepository().Authentication.State.skipWhile(state => state === Authentication.LoginState.Pending)
            .first()
            .flatMap(state => {
                const subject = new Subject<SavedContent<T>>();
                const formData = new FormData();
                formData.append(File.name || 'File', File);
                formData.set('ChunkToken', '0*0*False*False');
                formData.set('FileName', File.name)
 
 
                const request = new XMLHttpRequest();
                request.withCredentials = true;
                const path = ODataHelper.joinPaths(scope.GetRepository().ODataBaseUrl, scope.GetFullPath(), 'upload');
                request.open('POST', path);
 
                request.onreadystatechange = () => {
                    if (request.readyState === 4) {
 
                        switch (request.status) {
                            case 200:
                                try {
                                    const contentId = JSON.parse(request.response).Id;
                                    scope.GetRepository().Load(contentId, undefined, ContentType).subscribe(c => {
                                        subject.next(c);
                                        subject.complete();
                                        scope.GetRepository().Events.Trigger.ContentCreated({ Content: c });
                                    }, err => {
                                        scope.GetRepository().Events.Trigger.ContentCreateFailed({ Error: err, Content: { Name: File.name } as any });
                                        subject.error(err);
 
                                    })
                                } catch (error) {
                                    scope.GetRepository().Events.Trigger.ContentCreateFailed({ Error: error, Content: { Name: File.name } as any });
                                    subject.error(error);
                                }
                                break;
                            default:
                                subject.error({ message: 'Invalid Request status', request })
                        }
                    }
                }
                request.send(formData);
                return subject.asObservable();
            })
    }
}