import axios, { AxiosResponse } from 'axios'; import { DatabaseModel, GetInfo, MessageResponse, ProjectData, ProjectFileListResponse } from 'ontime-types'; import { makeCSV, makeTable } from '../../features/cuesheet/cuesheetUtils'; import { apiEntryUrl } from './constants'; import { createBlob, downloadBlob } from './utils'; const dbPath = `${apiEntryUrl}/db`; /** * HTTP request to the current DB */ async function getDb(filename: string): Promise> { return axios.post(`${dbPath}/download/`, { filename }); } /** * Request download of the current project file * @param fileName */ export async function downloadProject(fileName: string) { try { const { data, name } = await fileDownload(fileName); const fileContent = JSON.stringify(data, null, 2); const blob = createBlob(fileContent, 'application/json;charset=utf-8;'); downloadBlob(blob, `${name}.json`); } catch (error) { console.error(error); } } /** * Request download of the current rundown as a CSV file * @param fileName */ export async function downloadCSV(fileName: string = 'rundown') { try { const { data, name } = await fileDownload(fileName); const { project, rundown, customFields } = data; const sheetData = makeTable(project, rundown, customFields); const fileContent = makeCSV(sheetData); const blob = createBlob(fileContent, 'text/csv;charset=utf-8;'); downloadBlob(blob, `${name}.csv`); } catch (error) { console.error(error); } } /** * HTTP request to upload project file */ export async function uploadProjectFile(file: File): Promise { const formData = new FormData(); formData.append('project', file); const response = await axios.post(`${dbPath}/upload`, formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); return response.data; } /** * Make patch changes to the objects in the db */ export async function patchData(patchDb: Partial): Promise> { return await axios.patch(dbPath, patchDb); } /** * HTTP request to create a project file */ export async function createProject( project: Partial< ProjectData & { filename: string; } >, ): Promise { const res = await axios.post(`${dbPath}/new`, project); return res.data; } /** * HTTP request to get the list of available project files */ export async function getProjects(): Promise { const res = await axios.get(`${dbPath}/all`); return res.data; } /** * HTTP request to load a project file */ export async function loadProject(filename: string): Promise { const res = await axios.post(`${dbPath}/load`, { filename, }); return res.data; } /** * HTTP request to duplicate a project file */ export async function duplicateProject(filename: string, newFilename: string): Promise { const url = `${dbPath}/${filename}/duplicate`; const decodedUrl = decodeURIComponent(url); const res = await axios.post(decodedUrl, { newFilename, }); return res.data; } /** * HTTP request to rename a project file */ export async function renameProject(filename: string, newFilename: string): Promise { const url = `${dbPath}/${filename}/rename`; const decodedUrl = decodeURIComponent(url); const res = await axios.put(decodedUrl, { filename: newFilename, }); return res.data; } /** * HTTP request to delete a project file */ export async function deleteProject(filename: string): Promise { const url = `${dbPath}/${filename}`; const decodedUrl = decodeURIComponent(url); const res = await axios.delete(decodedUrl); return res.data; } /** * HTTP request to retrieve application info */ export async function getInfo(): Promise { const res = await axios.get(`${dbPath}/info`); return res.data; } /** * Utility function gets project from db * @param fileName * @returns */ async function fileDownload(fileName: string): Promise<{ data: DatabaseModel; name: string }> { const response = await getDb(fileName); const headerLine = response.headers['Content-Disposition']; // try and get the filename from the response let name = fileName; if (headerLine != null) { const startFileNameIndex = headerLine.indexOf('"') + 1; const endFileNameIndex = headerLine.lastIndexOf('"'); name = headerLine.substring(startFileNameIndex, endFileNameIndex); } return { data: response.data, name }; }