/** * Copyright 2022 Gravwell, Inc. All rights reserved. * * Contact: [legal@gravwell.io](mailto:legal@gravwell.io) * * This software may be modified and distributed under the terms of the MIT * license. See the LICENSE file for details. */ import * as FormData from 'form-data'; import { isString, pick } from 'lodash'; import { CreatableFile } from '~/models/file/creatable-file'; import { FileMetadata } from '~/models/file/file-metadata'; import { RawBaseFileMetadata } from '~/models/file/raw-file-metadata'; import { toRawCreatableFile } from '~/models/file/to-raw-creatable-file'; import { APIContext } from '../utils/api-context'; import { buildHTTPRequestWithAuthFromContext } from '../utils/build-http-request'; import { buildURL } from '../utils/build-url'; import { HTTPRequestOptions } from '../utils/http-request-options'; import { parseJSONResponse } from '../utils/parse-json-response'; import { makeGetOneFileDetails } from './get-one-file-details'; import { makeUpdateOneFile } from './update-one-file'; export const makeCreateOneFile = (context: APIContext): ((data: CreatableFile) => Promise) => { const updateOneFile = makeUpdateOneFile(context); const getOneFile = makeGetOneFileDetails(context); const filesPath = '/api/files'; const url = buildURL(filesPath, { ...context, protocol: 'http' }); return async (data: CreatableFile): Promise => { try { const baseRequestOptions: HTTPRequestOptions = { body: toFormData(data) as any, }; const req = buildHTTPRequestWithAuthFromContext(context, baseRequestOptions); const raw = await context.fetch(url, { ...req, method: 'POST' }); const rawRes = await parseJSONResponse(raw); const fileID = rawRes.ThingUUID; // !WARNING: Can't set all properties on file creation gravwell/gravwell#2506 const updateProps: Array = ['globalID', 'groupIDs', 'userID', 'isGlobal', 'labels']; const needsUpdate = Object.keys(data).some(k => updateProps.includes(k as keyof CreatableFile)); if (needsUpdate) { return updateOneFile({ ...pick(data, updateProps), id: fileID }); } return getOneFile(fileID); } catch (err) { if (err instanceof Error) { throw err; } throw Error('Unknown error'); } }; }; const toFormData = (data: CreatableFile): FormData => { const raw = toRawCreatableFile(data); const formData = new FormData(); if (isString(raw.guid)) { formData.append('guid', raw.guid); } formData.append('name', raw.name); formData.append('desc', raw.desc); formData.append('file', raw.file); return formData; };