Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

3.6 KB
Raw Blame
import type { FilesystemStorageDriverOptions } from '../../storage.types';
import type { StorageDriver } from '../drivers.models';
import fs from 'node:fs';
import { dirname, join } from 'node:path';
import { safely } from '@corentinth/chisels';
import { isFileAlreadyExistsError } from '../../../shared/fs/fs.models';
import { checkFileExists, deleteFile, ensureDirectoryExists } from '../../../shared/fs/fs.services';
import {
  createFileAlreadyExistsInStorageError,
  createFileNotFoundError,
} from '../../storage.errors';
import { isFileNotFoundError } from './fs.storage-driver.models';

export const FS_STORAGE_DRIVER_NAME = 'filesystem' as const;

export const fsStorageDriverFactory = ({ root }: FilesystemStorageDriverOptions) => {
  const getStoragePath = ({ storageKey }: { storageKey: string }) => ({
    storagePath: join(root, storageKey),
  });

  return {
    name: FS_STORAGE_DRIVER_NAME,
    saveFile: async ({ fileStream, storageKey }) => {
      const { storagePath } = getStoragePath({ storageKey });

      await ensureDirectoryExists({ path: dirname(storagePath) });

      // 'wx' flag ensures that the file is created exclusively and fails if it already exists
      const writeStream = fs.createWriteStream(storagePath, { flags: 'wx' });

      fileStream.pipe(writeStream);

      return new Promise((resolve, reject) => {
        writeStream.on('finish', () => {
          resolve();
        });

        writeStream.on('error', (error) => {
          const rejection = isFileAlreadyExistsError({ error })
            ? createFileAlreadyExistsInStorageError()
            : error;

          reject(rejection);
        });

        // Listen for errors on the input stream as well
        fileStream.on('error', (error) => {
          // Clean up the write stream and file
          writeStream.destroy();
          fs.unlink(storagePath, () => {}); // Ignore errors when cleaning up
          reject(error);
        });
      });
    },
    copyFile: async ({ sourceStorageKey, destinationStorageKey }) => {
      const { storagePath: sourcePath } = getStoragePath({ storageKey: sourceStorageKey });
      const { storagePath: destinationPath } = getStoragePath({
        storageKey: destinationStorageKey,
      });

      await ensureDirectoryExists({ path: dirname(destinationPath) });

      const [, error] = await safely(
        fs.promises.copyFile(sourcePath, destinationPath, fs.constants.COPYFILE_EXCL),
      );

      if (error && isFileNotFoundError({ error })) {
        throw createFileNotFoundError();
      }

      if (error && isFileAlreadyExistsError({ error })) {
        throw createFileAlreadyExistsInStorageError();
      }

      if (error) {
        throw error;
      }
    },
    getFileStream: async ({ storageKey }) => {
      const { storagePath } = getStoragePath({ storageKey });

      const fileExists = await checkFileExists({ path: storagePath });

      if (!fileExists) {
        throw createFileNotFoundError();
      }

      const fileStream = fs.createReadStream(storagePath);

      return { fileStream };
    },
    deleteFile: async ({ storageKey }) => {
      const { storagePath } = getStoragePath({ storageKey });

      const [, error] = await safely(deleteFile({ filePath: storagePath }));

      if (error && isFileNotFoundError({ error })) {
        throw createFileNotFoundError();
      }

      if (error) {
        throw error;
      }
    },
    fileExists: async ({ storageKey }) => {
      const { storagePath } = getStoragePath({ storageKey });
      const exists = await checkFileExists({ path: storagePath });

      return exists;
    },
  } satisfies StorageDriver;
};