import {Inject, Injectable} from "@angular/core"; import {File} from "../../../api/models/File"; import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser"; import {SortKey} from "../../models/SortKey"; import {MediarepoApi} from "../../../api/Api"; import {mapMany, mapNew} from "../../../api/models/adaptors"; import {FileMetadata} from "../../../api/api-types/files"; import {SearchFilters} from "../../../api/models/SearchFilters"; @Injectable({ providedIn: "root" }) export class FileService { constructor( @Inject(DomSanitizer) private sanitizer: DomSanitizer, ) { } public async getAllFiles(): Promise { return MediarepoApi.getAllFiles().then(mapMany(mapNew(File))); } public async findFiles(filters: SearchFilters, sortBy: SortKey[]): Promise { return MediarepoApi.findFiles( { filters: filters.getFilters(), sortBy: sortBy.map(k => k.toBackendType()) }) .then(mapMany(mapNew(File))); } public async getFileMetadata(id: number): Promise { return MediarepoApi.getFileMetadata({ id }); } public async updateFileName(id: number, name: string): Promise { return MediarepoApi.updateFileName({ id, name }); } /** * Builds a safe thumbnail url that accesses custom scheme for thumbnails * @param {File} file * @param {number} height * @param {number} width * @returns {SafeResourceUrl} */ public buildThumbnailUrl(file: File, height: number, width: number): SafeResourceUrl { return this.sanitizer.bypassSecurityTrustResourceUrl( `thumb://${file.cd}?width=${250}&height=${250}`); } /** * Builds a safe content url that accesses custom scheme for thumbnails * @param {File} file * @returns {SafeResourceUrl} */ public buildContentUrl(file: File): SafeResourceUrl { return this.sanitizer.bypassSecurityTrustResourceUrl( `content://${file.cd}`); } /** * Saves a file locally * @param {File} file * @param {string} targetPath * @returns {Promise} */ public async saveFile(file: File, targetPath: string) { await MediarepoApi.saveFileLocally({ id: file.id, path: targetPath }); } /** * Deletes all thumbnails of a file * @param {File} file * @returns {Promise} */ public async deleteThumbnails(file: File) { await MediarepoApi.deleteThumbnails({ id: file.id }); } /** * Reads the contents of a file and returns the object url for it * @param {File} file * @returns {Promise} */ public async readFile(file: File): Promise { const data = await MediarepoApi.readFile( { mimeType: file.mimeType, hash: file.cd }); const blob = new Blob([new Uint8Array(data)], { type: file.mimeType }); const url = URL?.createObjectURL(blob); return this.sanitizer.bypassSecurityTrustResourceUrl(url); } }