File size: 3,672 Bytes
d988ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { FilesystemService } from '../common/filesystem/filesystem.service';
import { ClipboardService } from '../clipboard/clipboard.service';
import { v4 as uuidv4 } from 'uuid';

export interface FileEntry {
  id: string;
  filename: string;
  mimetype: string;
  size: number;
  storageKey: string;
  createdAt: string;
  createdBy: string;
}

@Injectable()
export class FileService {
  private readonly logger = new Logger(FileService.name);

  constructor(
    private readonly filesystemService: FilesystemService,
    private readonly clipboardService: ClipboardService,
  ) {}

  async uploadFile(
    roomCode: string,
    file: Express.Multer.File,
    clientId: string,
  ): Promise<FileEntry> {
    // Check if clipboard exists
    const clipboard = await this.clipboardService.getClipboard(roomCode);
    if (!clipboard) {
      throw new NotFoundException(`Clipboard ${roomCode} not found`);
    }

    // Generate unique ID for file
    const fileId = uuidv4();
    const fileName = `${fileId}-${file.originalname}`;

    // Upload file to filesystem
    await this.filesystemService.uploadFile(file, roomCode, fileName);

    // Create file entry
    const fileEntry: FileEntry = {
      id: fileId,
      filename: file.originalname,
      mimetype: file.mimetype,
      size: file.size,
      storageKey: `${roomCode}/${fileName}`,
      createdAt: new Date().toISOString(),
      createdBy: clientId,
    };

    // Add file to clipboard
    if (!clipboard.files) {
      clipboard.files = [];
    }
    clipboard.files.unshift(fileEntry);
    clipboard.lastActivity = new Date().toISOString();

    // Save updated clipboard
    await this.clipboardService.saveClipboard(roomCode, clipboard);

    return fileEntry;
  }

  async getFileUrl(roomCode: string, fileId: string): Promise<string> {
    const fileData = await this.getFileData(roomCode, fileId);
    return fileData.url;
  }

  async getFileData(roomCode: string, fileId: string): Promise<{ url: string; filename: string; fileEntry: FileEntry }> {
    const clipboard = await this.clipboardService.getClipboard(roomCode);
    if (!clipboard || !clipboard.files) {
      throw new NotFoundException(`Clipboard ${roomCode} not found`);
    }

    const fileEntry = clipboard.files.find(file => file.id === fileId);
    if (!fileEntry) {
      throw new NotFoundException(`File ${fileId} not found`);
    }

    // Return the URL to the file controller with the correct API path
    const [roomCodeFromPath, fileName] = fileEntry.storageKey.split('/');
    return {
      url: `/api/files/${roomCodeFromPath}/${fileName}`,
      filename: fileEntry.filename,
      fileEntry
    };
  }

  async deleteFile(roomCode: string, fileId: string): Promise<boolean> {
    const clipboard = await this.clipboardService.getClipboard(roomCode);
    if (!clipboard || !clipboard.files) {
      throw new NotFoundException(`Clipboard ${roomCode} not found`);
    }

    const fileIndex = clipboard.files.findIndex(file => file.id === fileId);
    if (fileIndex === -1) {
      throw new NotFoundException(`File ${fileId} not found`);
    }

    const fileEntry = clipboard.files[fileIndex];

    // Delete file from filesystem
    const [roomCodeFromPath, fileName] = fileEntry.storageKey.split('/');
    await this.filesystemService.deleteFile(roomCodeFromPath, fileName);

    // Remove file entry from clipboard
    clipboard.files.splice(fileIndex, 1);
    clipboard.lastActivity = new Date().toISOString();

    // Save updated clipboard
    await this.clipboardService.saveClipboard(roomCode, clipboard);

    return true;
  }
}