'use strict';
import _0x1869d0__default from 'path';
import { mediaConstants } from '../../meta/constants/media-constants.js';
import { apiNotFoundResponse, apiBadRequestResponse, apiNoContentResponse, getImageResponse, apiOkResponse, apiCreatedResponse, apiInternalServerErrorResponse } from '../../meta/server/web/responses.js';
import { metaErrorMessages } from '../../meta/constants/meta-error-messages-constants.js';
import { isString, isObject } from '../../../fd-toolbox/types/ensure-type.js';
import { promises, createWriteStream } from 'fs';
import { deleteBlobByUrl } from '../blob/blob-db.js';
import { Readable } from 'stream';
import { combineEndpointPath } from '../../../fd-toolbox/http/url/urls.js';
import { supportedVideoTypes, supportedImageTypes } from '../../../fd-toolbox/constants/file-loader-data-types.js';
import { prohibitedFileType } from './media-error-message-constants.js';
import { isUsingS3 } from '../../../fd-toolbox/lib/environments.js';
import { responseMessages } from '../../../fd-toolbox/server/web/response-messages.js';
import { getRemoteBlobContent, uploadRemoteBlob } from '../integrations/remote-blob/remote-blob-storage.js';
import { remoteBlobConstants } from '../../../base-repo/constants/remote-blob-constants.js';
import { httpContentTypes } from '../../../fd-toolbox/constants/header-names.js';
import { wellKnownMetaProps } from '../../../fd-toolbox-core/constants/meta-constants.js';
import { createRemoteBlobResponse } from '../web/responses/create-remote-blob-response.js';
import { parseFormDataRequest } from '../../../fd-toolbox/http/http.js';
import { metaQueryParams } from '../../../fd-toolbox/constants/meta-query-params.js';
import { childProcessEvents } from '../../../fd-toolbox/constants/child-process-events.js';
import { contentFormatElements } from '../../../fd-toolbox/constants/html-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x3c5118,_0x1ebca2)=>__defProp(_0x3c5118,'name',{'value':_0x1ebca2,'configurable':!![]});const defaultType=httpContentTypes['contentTypeHeaderOctetStream'],mediaCategories={'default':metaQueryParams['default'],'video':contentFormatElements['video'],'image':contentFormatElements['image']},allowedTypesMap=new Map([[defaultType,mediaCategories['default']],...supportedVideoTypes['map'](_0x308b58=>[_0x308b58,mediaCategories['video']]),...supportedImageTypes['map'](_0x3ce137=>[_0x3ce137,mediaCategories['image']])]);function isAllowedFileType(_0xd99f40){return allowedTypesMap['has'](_0xd99f40);}__name(isAllowedFileType,'isAllowedFileType');async function mediaGetRoute(_0x25a153){const _0x288e62=getBlobName(_0x25a153);if(!_0x288e62)return apiNotFoundResponse({'message':responseMessages['notFoundError']});const _0x4115cd=isUsingS3()?await getRemoteBlob(_0x288e62):await getLocalBlob(_0x288e62);return _0x4115cd;}__name(mediaGetRoute,'mediaGetRoute');async function mediaPostRoute(_0x1ff54c){const _0x227caa=await parseFormDataRequest(_0x1ff54c,_0x43a536=>({'file':_0x43a536['get'](mediaConstants['fileKey'])??void(-0x64*-0x27+0x74b+-0x1687),'filename':_0x43a536['get'](wellKnownMetaProps['name'])??void(0x165d*0x1+0x3*0x493+-0x1*0x2416)}));if(!isString(_0x227caa['filename']))return apiBadRequestResponse(responseMessages['missingFileName']);const _0x52d23f=isUsingS3()?await uploadToS3(_0x227caa['filename'],_0x227caa['file']):await saveLocalBlob(_0x227caa['filename'],_0x227caa['file']);return _0x52d23f;}__name(mediaPostRoute,'mediaPostRoute');async function mediaDeleteRoute(_0x3a1eba){const _0x21da76=new URL(_0x3a1eba['url'])['searchParams']['get'](remoteBlobConstants['blobNameParam']);if(!_0x21da76)return apiNotFoundResponse({'message':responseMessages['notFoundError']});const _0x8766f=_0x1869d0__default['join'](mediaConstants['path'],_0x21da76),_0xd9bd2a=combineEndpointPath(mediaConstants['blob'],_0x21da76);return promises['rm'](_0x8766f),await deleteBlobByUrl(_0xd9bd2a),apiNoContentResponse();}__name(mediaDeleteRoute,'mediaDeleteRoute');function getBlobName(_0x57b1d5){const _0x5c05c5=new URL(_0x57b1d5['url'])['searchParams']['get'](remoteBlobConstants['blobNameParam']);return _0x5c05c5;}__name(getBlobName,'getBlobName');async function getRemoteBlob(_0x331a0a){const _0x5e588d=await getRemoteBlobContent(_0x331a0a);if(!_0x5e588d)return apiNotFoundResponse({'message':responseMessages['notFoundError']});const _0x30b766={'content':_0x5e588d['content'],'metadata':{'contentType':_0x5e588d['contentType']||httpContentTypes['contentTypeHeaderOctetStream'],'blobName':_0x5e588d['blobName']}};return createRemoteBlobResponse(_0x30b766);}__name(getRemoteBlob,'getRemoteBlob');async function getLocalBlob(_0xb53543){const _0x1532e5=_0x1869d0__default['join'](mediaConstants['path'],_0xb53543);return getImageResponse(_0x1532e5)['catch'](()=>apiNotFoundResponse({'message':metaErrorMessages['fileNotFound']['replace']('{0}',_0x1532e5)}));}__name(getLocalBlob,'getLocalBlob');async function uploadToS3(_0x37017a,_0x59d828){let _0x41e962;if(!isObject(_0x59d828)||!(_0x59d828 instanceof File))_0x41e962=apiBadRequestResponse(metaErrorMessages['fileIsNotProvided']);else {const _0x14e932=await uploadRemoteBlob(_0x37017a,_0x59d828['type'],await _0x59d828['arrayBuffer']());!_0x14e932?_0x41e962=apiBadRequestResponse(metaErrorMessages['fileIsNotProvided']):_0x41e962=apiOkResponse(_0x14e932);}return _0x41e962;}__name(uploadToS3,'uploadToS3');async function saveLocalBlob(_0x11022f,_0x291bf6){let _0x3197e3;if(!isObject(_0x291bf6)||!(_0x291bf6 instanceof Blob))_0x3197e3=apiBadRequestResponse(metaErrorMessages['fileIsNotProvided']);else {if(!isAllowedFileType(_0x291bf6['type']))_0x3197e3=apiBadRequestResponse(prohibitedFileType['replace']('{0}',Object['keys'](mediaCategories)['join'](',\x20')));else {const _0x5b845c=_0x1869d0__default['join'](mediaConstants['path'],_0x11022f),_0x3357a9=createWriteStream(_0x5b845c),_0x595f89=Readable['from']((async function*(){const _0x295584=_0x291bf6['stream']()['getReader']();let _0x54b450;do{_0x54b450=await _0x295584['read']();if(!_0x54b450['done'])yield _0x54b450['value'];}while(!_0x54b450['done']);}()));_0x595f89['pipe'](_0x3357a9),_0x3197e3=await new Promise(_0x5688e8=>{_0x3357a9['on'](childProcessEvents['finish'],()=>_0x5688e8(apiCreatedResponse(combineEndpointPath(mediaConstants['blob'],_0x11022f)))),_0x3357a9['on'](childProcessEvents['error'],()=>_0x5688e8(apiInternalServerErrorResponse(metaErrorMessages['fileCouldNotBeSaved'])));});}}return _0x3197e3;}__name(saveLocalBlob,'saveLocalBlob');

export { mediaDeleteRoute, mediaGetRoute, mediaPostRoute };
