All files / modules upload.ts

100% Statements 42/42
80% Branches 8/10
100% Functions 13/13
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1245x   5x   5x 5x 5x         5x   5x                 5x 4x             4x                     4x 4x   4x 4x 4x       4x 4x 4x   3x 2x     1x       1x                               5x 12x             12x                   4x 4x 4x   3x 2x 2x 2x 2x 22x   2x     1x       1x                  
import { Injectable } from '@angular/core';
 
import { take } from 'rxjs/operators';
 
import { AppConstants} from '@unpispas/upp-defs';
import { httpService } from './http';
import { languageService } from './language';
 
/**
 * Enum representing the source of the upload.
 */
export enum uploadSource {
    /** Attachment upload source. */
    attach = 'attach'
}
 
/**
 * Service for handling file uploads to a remote server.
 */
@Injectable({
    providedIn: 'root'
})
export class uploadService {
    private uploadUrl = AppConstants.baseURL + 'common/upload.php';
    
    /**
     * Creates an instance of uploadService.
     * @param http - The HTTP service for making requests.
     * @param lang - The language service for handling translations.
     */    
    constructor(private http: httpService, private lang: languageService){
        // nothing to do
    }
    
    /**
     * Uploads a file to the server.
     * @param data - The data to upload, typically file data.
     * @param source - The source of the upload, defaults to `null`.
     * @returns A Promise that resolves with the uploaded file name or rejects with an error message.
     */    
    post(data: any, source: uploadSource | null = null){
        return new Promise((resolve, reject) => {
            const formData = new FormData();
            
            for (const field in data) {
                if (Object.prototype.hasOwnProperty.call(data, field)){
                    formData.append(field, data[field], data[field].name); 
                }
            }
                
            const _getparams = (source) ? '?source=' + source : '';            
            const _request = this.http.post(this.uploadUrl + _getparams, formData, httpService.headers, 'json', 'multipart')
            _request.pipe(take(1)).subscribe({
                next: (data: any) => {
                    if (data['errorcode'] == 0){
                        resolve(data['filename']);
                    }
                    else {
                        reject(this.lang.tr('@service_request_error_' + data['errorcode']));
                    }    
                },
                error: (error) => {
                    reject(this.lang.tr('@http_request_error', [ error.status ]));
                },
                complete: () => {
                    // nothing to do
                }
            });
        });
    };
}
 
/**
 * Service for handling file downloads from a remote server.
 */
@Injectable({
    providedIn: 'root'
})
export class downloadService {
    private _doDownloadUrl = 'common/download.php';
    
    /**
     * Creates an instance of downloadService.
     * @param http - The HTTP service for making requests.
     * @param lang - The language service for handling translations.
     */    
    constructor(private http: httpService, private lang: languageService){
        // nothing to do
    }
    
    /**
     * Downloads a file from the server.
     * @param url - The URL of the file to download.
     * @returns A Promise that resolves with the file's binary data as an ArrayBuffer or rejects with an error message.
     */    
    file(url: string){
        return new Promise<ArrayBuffer>((resolve, reject) => {
            const _request = this.http.get(AppConstants.baseURL + this._doDownloadUrl + "?url=" + encodeURIComponent(url));
            _request.pipe(take(1)).subscribe({
                next: (data: any) => {
                    if (data['errorcode'] == 0){
                        const _binary = atob(data['base64']);
                        const _length = _binary.length;                
                        const _bytes = new Uint8Array(_length);
                        for (let i = 0; i < _length; i++) {
                            _bytes[i] = _binary.charCodeAt(i);
                        }
                        resolve(_bytes.buffer);
                    }
                    else {
                        reject(this.lang.tr('@service_request_error_' + data['errorcode']));
                    }    
                },
                error: (error) => {
                    reject(this.lang.tr('@http_request_error', [ error.status ]));
                },
                complete: () => {
                    // nothing to do
                }
            });
        });
    }
}