All files / src/platform/documents/providers local-storage.provider.ts

0% Statements 0/23
0% Branches 0/6
0% Functions 0/4
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                                                                                   
import { Injectable, Logger } from '@nestjs/common';
import * as fs from 'fs';
import * as path from 'path';
import { StorageProvider } from '../storage-provider.interface';
 
@Injectable()
export class LocalStorageProvider implements StorageProvider {
  private readonly logger = new Logger(LocalStorageProvider.name);
  private readonly baseDir = path.join(
    __dirname,
    '..',
    '..',
    '..',
    '..',
    'uploads',
  );
 
  constructor() {
    if (!fs.existsSync(this.baseDir)) {
      fs.mkdirSync(this.baseDir, { recursive: true });
    }
  }
 
  async upload(key: string, buffer: Buffer): Promise<string> {
    const filePath = path.join(this.baseDir, key);
    const dir = path.dirname(filePath);
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir, { recursive: true });
    }
 
    fs.writeFileSync(filePath, buffer);
    this.logger.log(`File uploaded to local storage: ${filePath}`);
    return key;
  }
 
  async getDownloadUrl(key: string, expiresSeconds: number): Promise<string> {
    // Return relative or mock URL for dev download endpoints
    return `/api/v1/files/download-raw/${key}`;
  }
 
  async delete(key: string): Promise<boolean> {
    const filePath = path.join(this.baseDir, key);
    if (fs.existsSync(filePath)) {
      fs.unlinkSync(filePath);
      return true;
    }
    return false;
  }
}