import { Injectable } from '@angular/core'; /** * handles assets requests * * ensure the requested assets is valid or returns a valid alternative * atleast try to save the situation * fails silently, if the matter cannot be helped */ @Injectable() export class AssetService{ location: string = 'assets'; type: 'image' | 'video' | 'audio' | 'text' = 'image'; defaultName?: string; group: string; name: string; changeLocation(newLocation: string){ this.location = newLocation; return this; } default(defaultName: string){ this.defaultName = defaultName; return this; } image(name: string, defaultName?: string){ return this.make(this.location, 'image', name, defaultName); } video(name: string, defaultName?: string){ return this.make(this.location, 'video', name, defaultName); } audio(name: string, defaultName?: string){ return this.make(this.location, 'audio', name, defaultName); } /** * * @param location * @param type must be the singular of the corresponding directory name * @param defaultName * @param name */ make(location: string, type: 'image' | 'video' | 'audio' | 'text', name: string, defaultName?: string){ this.location = location; this.type = type; this.defaultName = defaultName; this.name = name; let path = this.location + '/' + this.type + 's/'; // path += (!this.check(path) && this.defaultName) ? 'default/' + this.defaultName : this.name; path += (!this.name && this.defaultName) ? 'default/' + this.defaultName : this.name; return path; } /** * check if the image url is valid * returns true if the url is valid * * use to decide weather to return a default alternative for an image request */ check(path: string){ return true; } }