OpenSlides/client/src/app/core/ui-services/media-manage.service.ts

124 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-11-16 16:09:15 +01:00
import { Injectable } from '@angular/core';
2018-11-16 16:09:15 +01:00
import { Observable } from 'rxjs';
import { HttpService } from 'app/core/core-services/http.service';
import { ConfigService } from 'app/core/ui-services/config.service';
import { ViewMediafile } from '../../site/mediafiles/models/view-mediafile';
2018-11-16 16:09:15 +01:00
/**
* The structure of an image config object
*/
interface ImageConfigObject {
2019-01-10 12:54:48 +01:00
display_name: string;
2018-11-16 16:09:15 +01:00
key: string;
path: string;
}
/**
* The structure of a font config
*/
interface FontConfigObject {
2019-01-10 12:54:48 +01:00
display_name: string;
2018-11-16 16:09:15 +01:00
default: string;
path: string;
}
/**
* Holds the required structure of the manage payload
*/
interface ManagementPayload {
2019-01-10 12:54:48 +01:00
id: number;
key?: string;
default?: string;
value: ImageConfigObject | FontConfigObject;
2018-11-16 16:09:15 +01:00
}
/**
* The service to manage Mediafiles.
*
* Declaring images as logos (web, projector, pdf, ...) is handles here.
*/
@Injectable({
2019-01-10 12:54:48 +01:00
providedIn: 'root'
2018-11-16 16:09:15 +01:00
})
export class MediaManageService {
/**
* Constructor for the MediaManage service
*
* @param httpService OpenSlides own HttpService
*/
public constructor(private config: ConfigService, private httpService: HttpService) {}
/**
* Sets the given Mediafile to using the given management option
* i.e: setting another projector logo
*
* TODO: Feels overly complicated. However, the server seems to requires a strictly shaped payload
*
* @param file the selected Mediafile
* @param action determines the action
*/
public async setAs(file: ViewMediafile, action: string): Promise<void> {
2019-01-22 11:59:16 +01:00
const restPath = `rest/core/config/${action}/`;
2018-11-16 16:09:15 +01:00
const config = this.getMediaConfig(action);
2019-01-10 12:54:48 +01:00
const path = config.path !== file.downloadUrl ? file.downloadUrl : '';
2018-11-16 16:09:15 +01:00
// Create the payload that the server requires to manage a mediafile
const payload: ManagementPayload = {
id: file.id,
key: (config as ImageConfigObject).key,
default: (config as FontConfigObject).default,
value: {
display_name: config.display_name,
key: (config as ImageConfigObject).key,
default: (config as FontConfigObject).default,
path: path
}
2019-01-10 12:54:48 +01:00
};
2018-11-16 16:09:15 +01:00
return this.httpService.put<void>(restPath, payload);
}
2019-01-16 15:54:34 +01:00
/**
* Checks if an image is an imageConfig Object
*
* @param object instance of something to check
* @returns boolean if an object is a ImageConfigObject
*/
public isImageConfigObject(object: any): object is ImageConfigObject {
if (object !== undefined) {
return (<ImageConfigObject>object).path !== undefined;
}
}
2018-11-16 16:09:15 +01:00
/**
* Get all actions that can be executed on images
*
* @returns observable array of strings with the actions for images
*/
public getLogoActions(): Observable<string[]> {
return this.config.get('logos_available');
}
/**
* Get all actions that can be executed on fonts
*
* @returns observable array of string with the actions for fonts
*/
public getFontActions(): Observable<string[]> {
return this.config.get('fonts_available');
}
/**
* returns the config object to a given action
*
* @param action the logo action or font action
* @returns A media config object containing the requested values
*/
public getMediaConfig(action: string): ImageConfigObject | FontConfigObject {
return this.config.instant(action);
}
}