All files / src BinaryField.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 6/6
100% Lines 12/12
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 66 67                          1x             411x     4x   1x                     411x           4x 2x   2x             3x                 411x 411x 411x      
/**
 * @module Content
 */ /** */
 
import { Observable } from 'rxjs/Observable';
import { MediaResourceObject } from './ComplexTypes';
import { IContent, SavedContent } from './Content';
import { BinaryFieldSetting } from './FieldSettings';
import { UploadProgressInfo } from './Repository/UploadModels';
 
/**
 * Represents a binary field instance
 */
export class BinaryField<T extends IContent> {
 
    /**
     * Saves a File object instance (from a form input or drop event) into the binary field
     * @param {File} file The file to be saved
     * @returns {Observable<UploadProgressInfo<T>>} An observable that will update with the upload progress
     */
    public SaveBinaryFile: (file: File) => Observable<UploadProgressInfo<T>>
    = (file: File) =>
 
        this._contentReference.GetRepository().UploadFile({
            File: new File([file], this._contentReference.Name),
            Parent: {GetFullPath: () => this._contentReference.ParentContentPath, Path: this._contentReference.ParentPath} as SavedContent,
            PropertyName: this._fieldSettings.Name,
            ContentType: this._contentReference.constructor as { new(...args: any[]): T },
            Overwrite: true,
        })
 
    /**
     * Saves a text from a string variable into the Binary field
     * @param {string} text The text to be saved
     * @returns {Observable<UploadProgressInfo<T>>} An observable that will update with the upload progress
     */
    public SaveBinaryText: (text: string) => Observable<UploadProgressInfo<T>> = (text: string) => this.SaveBinaryFile(new File([text], this._contentReference.Name));
 
    /**
     * Returns the download URL for the binary
     */
    public GetDownloadUrl(): string {
        if (!this._mediaResourceObject || typeof this._mediaResourceObject !== 'object') {
            return `/binaryhandler.ashx?nodeid=${this._contentReference.Id}&propertyname=${this._fieldSettings.Name}`;
        }
        return this._mediaResourceObject.__mediaresource.media_src;
    }
 
    /**
     * returns the MediaResourceObject from the binary field
     */
    public GetMediaResourceObject(): MediaResourceObject {
        return Object.assign({}, this._mediaResourceObject);
    }
 
    /**
     *
     * @param {MediaResourceObject} _mediaResourceObject The media resource object from the content response
     * @param {T} _contentReference The owner content reference
     * @param {BinaryFieldSetting} _fieldSettings The corresponding fieldsettings
     */
    constructor(private readonly _mediaResourceObject: MediaResourceObject,
                private readonly _contentReference: SavedContent<T>,
                private readonly _fieldSettings: BinaryFieldSetting) {
    }
}