import { DriveFolderData , DriveFolderMetadataPayload , DriveItemData , FolderTree } from '../types' ; import errorService from '../../core/services/error.service' ; import { aes } from '@internxt/lib' ; import httpService from '../../core/services/http.service' ; import { DevicePlatform } from '../../core/types' ; import analyticsService from '../../analytics/services/analytics.service' ; import i18n from '../../i18n/services/i18n.service' ; import localStorageService from '../../core/services/local-storage.service' ; import { UserSettings } from '../../auth/types' ; import { createStorageClient } from '../../../factory/modules' ; import { StorageTypes } from '@internxt/sdk/dist/drive' ; import { CancelTokenSource } from 'axios' ; export interface IFolders { bucket : string ; color : string ; createdAt : Date ; encrypt_version : string ; icon : string ; iconId : number | null ; icon_id : number | null ; id : number ; name : string ; parentId : number ; parent_id : number ; updatedAt : Date ; userId : number ; user_id : number ; } export interface FolderChild { bucket : string ; color : string ; createdAt : string ; encrypt_version : string ; icon : string ; iconId : number | null ; icon_id : number | null ; id : number ; name : string ; parentId : number ; parent_id : number ; updatedAt : string ; userId : number ; user_id : number ; } export interface FetchFolderContentResponse { bucket : string ; children : FolderChild [ ] ; color : string ; createdAt : string ; encrypt_version : string ; files : DriveItemData [ ] ; icon : string ; id : number ; name : string ; parentId : number ; parent_id : number ; updatedAt : string ; userId : number ; user_id : number ; } export function createFolder ( currentFolderId , folderName ) { const payload = { parentFolderId : currentFolderId , folderName : folderName } ; const storageClient = createStorageClient ( ) ; const [ createdFolderPromise , cancelTokenSource ] = storageClient . createFolder ( payload ) ; const finalPromise = createdFolderPromise . then ( response => { const user = localStorageService . getUser ( ) as UserSettings ; analyticsService . trackFolderCreated ( { email : user . email , platform : DevicePlatform . Web , } ) ; return response ; } ) . catch ( error => { throw errorService . castError ( error ) ; } ) ; return [ finalPromise , cancelTokenSource ] ; } export async function updateMetaData ( folderId , metadata ) { const storageClient = createStorageClient ( ) ; const payload = { folderId : folderId , changes : metadata } ; return storageClient . updateFolder ( payload ) . then ( ( ) => { const user = localStorageService . getUser ( ) as UserSettings ; analyticsService . trackFolderRename ( { email : user . email , fileId : folderId , platform : DevicePlatform . Web , } ) ; } ) ; } export function deleteFolder ( folderData ) { const storageClient = createStorageClient ( ) ; return storageClient . deleteFolder ( folderData . id ) . then ( ( ) => { const user = localStorageService . getUser ( ) as UserSettings ; analyticsService . trackDeleteItem ( folderData as DriveItemData , { email : user . email , platform : DevicePlatform . Web , } ) ; } ) ; } async function fetchFolderTree ( folderId ) { const { tree , size } = await httpService . get < { tree : FolderTree ; size : number ; } > ( `/api/storage/tree/${ folderId }` ) ; const folderDecryptedNames = { } ; const fileDecryptedNames = { } ; const pendingFolders = [ tree ] ; while ( pendingFolders . length > 0 ) { const currentTree = pendingFolders [ 0 ] ; const { folders , files } = { folders : currentTree . children , files : currentTree . files , } ; folderDecryptedNames [ currentTree . id ] = aes . decrypt ( currentTree . name , `${ process . env . REACT_APP_CRYPTO_SECRET2 }-${ currentTree . parentId }` ) ; for ( const file of files ) { fileDecryptedNames [ file . id ] = aes . decrypt ( file . name , `${ process . env . REACT_APP_CRYPTO_SECRET2 }-${ file . folderId }` ) ; } pendingFolders . shift ( ) ; pendingFolders . push ( ... folders ) ; } return { tree , folderDecryptedNames , fileDecryptedNames , size } ; } export async function moveFolder ( folderId , destination ) { const storageClient = createStorageClient ( ) ; const payload = { folderId : folderId , destinationFolderId : destination } ; return storageClient . moveFolder ( payload ) . then ( response => { const user = localStorageService . getUser ( ) as UserSettings ; analyticsService . trackMoveItem ( 'folder' , { file_id : response . item . id , email : user . email , platform : DevicePlatform . Web , } ) ; return response ; } ) . catch ( ( err ) => { const castedError = errorService . castError ( err ) ; if ( castedError . status ) { castedError . message = i18n . get ( `tasks.move-folder.errors.${ castedError . status }` ) ; } throw castedError ; } ) ; } const folderService = { createFolder , updateMetaData , deleteFolder , moveFolder , fetchFolderTree , } ; export default folderService ;