# All files / services storage.service.ts

0% Statements 0/157
0% Branches 0/1
0% Functions 0/1
0% Lines 0/157

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
    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
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278

     /**
     * 统一存储服务
     * 支持 OSS 和本地存储的无缝切换
     */

    import { ossService } from './oss.service';
    import path from 'path';
    import fs from 'fs';
    import { v4 as uuidv4 } from 'uuid';

    export type StorageType = 'oss' | 'local';

    class StorageService {
      private storageType: StorageType;

      constructor() {
        // 从环境变量读取存储类型，默认 local
        this.storageType = (process.env.STORAGE_TYPE as StorageType) || 'local';
        console.log(`[Storage] 当前存储模式: ${this.storageType}`);
      }

      /**
       * 切换存储类型
       */
      setStorageType(type: StorageType) {
        this.storageType = type;
        console.log(`[Storage] 存储模式已切换为: ${type}`);
      }

      /**
       * 获取当前存储类型
       */
      getStorageType(): StorageType {
        return this.storageType;
      }

      /**
       * 上传音频文件
       * @param localPath 本地文件路径
       * @param audioId 音频 ID
       * @returns 文件 URL
       */
      async uploadAudio(localPath: string, audioId: string): Promise<string> {
        if (this.storageType === 'oss') {
          return await ossService.uploadAudio(localPath, audioId);
        } else {
          return this.uploadToLocal(localPath, 'audio', audioId);
        }
      }

      /**
       * 上传视频文件
       * @param localPath 本地文件路径
       * @param videoId 视频 ID
       * @returns 文件 URL
       */
      async uploadVideo(localPath: string, videoId: string): Promise<string> {
        if (this.storageType === 'oss') {
          return await ossService.uploadVideo(localPath, videoId);
        } else {
          return this.uploadToLocal(localPath, 'video', videoId);
        }
      }

      /**
       * 上传封面图片
       * @param localPath 本地文件路径
       * @param bookId 书籍 ID
       * @returns 文件 URL
       */
      async uploadCover(localPath: string, bookId: string | number): Promise<string> {
        if (this.storageType === 'oss') {
          return await ossService.uploadCover(localPath, bookId);
        } else {
          return this.uploadToLocal(localPath, 'cover', String(bookId));
        }
      }

      /**
       * 上传通用文件
       * @param localPath 本地文件路径
       * @param category 分类（audio/video/cover/material）
       * @param id ID
       * @returns 文件 URL
       */
      async uploadFile(localPath: string, category: string, id: string): Promise<string> {
        if (this.storageType === 'oss') {
          const objectKey = `${category}/${id}/${path.basename(localPath)}`;
          return await ossService.uploadFile(localPath, objectKey);
        } else {
          return this.uploadToLocal(localPath, category, id);
        }
      }

      /**
       * 上传 Buffer
       * @param buffer 文件 Buffer
       * @param objectKey 对象键
       * @param contentType 内容类型
       * @returns 文件 URL
       */
      async uploadBuffer(buffer: Buffer, objectKey: string, contentType?: string): Promise<string> {
        if (this.storageType === 'oss') {
          return await ossService.uploadBuffer(buffer, objectKey, contentType);
        } else {
          // 本地存储：保存到 uploads 目录
          const filePath = path.join(process.cwd(), 'uploads', objectKey);
          const dir = path.dirname(filePath);

          if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
          }

          fs.writeFileSync(filePath, buffer);
          return `/uploads/${objectKey}`;
        }
      }

      /**
       * 删除文件
       * @param url 文件 URL 或路径
       */
      async deleteFile(url: string): Promise<void> {
        if (this.storageType === 'oss') {
          // 从 URL 提取 objectKey
          const objectKey = this.extractObjectKey(url);
          if (objectKey) {
            await ossService.deleteFile(objectKey);
          }
        } else {
          // 本地存储：删除文件
          const filePath = this.urlToLocalPath(url);
          if (fs.existsSync(filePath)) {
            fs.unlinkSync(filePath);
          }
        }
      }

      /**
       * 删除整个目录
       * @param prefix 目录前缀或分类
       * @param id ID
       */
      async deleteDirectory(prefix: string, id: string): Promise<void> {
        if (this.storageType === 'oss') {
          await ossService.deleteDirectory(`${prefix}/${id}`);
        } else {
          const dirPath = path.join(process.cwd(), 'uploads', prefix, id);
          if (fs.existsSync(dirPath)) {
            fs.rmSync(dirPath, { recursive: true, force: true });
          }
        }
      }

      /**
       * 下载文件（用于音频下载功能）
       * @param url 文件 URL
       * @returns 文件 Buffer
       */
      async downloadFile(url: string): Promise<Buffer> {
        if (this.storageType === 'oss') {
          // OSS：从 OSS 下载
          const objectKey = this.extractObjectKey(url);
          if (!objectKey) {
            throw new Error('无效的 OSS URL');
          }
          return await ossService.downloadFile(objectKey);
        } else {
          // 本地存储：读取本地文件
          const filePath = this.urlToLocalPath(url);
          if (!fs.existsSync(filePath)) {
            throw new Error('文件不存在');
          }
          return fs.readFileSync(filePath);
        }
      }

      /**
       * 获取签名 URL（仅 OSS 支持）
       * @param url 文件 URL
       * @param expires 过期时间（秒）
       * @returns 签名 URL
       */
      async getSignedUrl(url: string, expires: number = 3600): Promise<string> {
        if (this.storageType === 'oss') {
          const objectKey = this.extractObjectKey(url);
          if (objectKey) {
            return await ossService.getSignedUrl(objectKey, expires);
          }
        }
        // 本地存储直接返回原 URL
        return url;
      }

      /**
       * 本地存储：上传文件
       */
      private uploadToLocal(localPath: string, category: string, id: string): string {
        const uploadDir = path.join(process.cwd(), 'uploads', category, id);

        // 确保目录存在
        if (!fs.existsSync(uploadDir)) {
          fs.mkdirSync(uploadDir, { recursive: true });
        }

        // 复制文件
        const filename = path.basename(localPath);
        const destPath = path.join(uploadDir, filename);
        fs.copyFileSync(localPath, destPath);

        // 返回访问 URL
        return `/uploads/${category}/${id}/${filename}`;
      }

      /**
       * 从 URL 提取 objectKey
       */
      private extractObjectKey(url: string): string | null {
        if (!url) return null;

        // OSS URL 格式: https://bucket.endpoint/objectKey
        if (url.includes('aliyuncs.com') || url.includes('oss-')) {
          try {
            const urlObj = new URL(url);
            return urlObj.pathname.substring(1); // 去掉开头的 /
          } catch {
            return null;
          }
        }

        // 本地 URL 格式: /uploads/category/id/filename
        if (url.startsWith('/uploads/')) {
          return url.substring('/uploads/'.length);
        }

        return null;
      }

      /**
       * URL 转本地路径
       */
      private urlToLocalPath(url: string): string {
        if (url.startsWith('/uploads/')) {
          return path.join(process.cwd(), 'uploads', url.substring('/uploads/'.length));
        }
        return url;
      }

      /**
       * 测试存储连接
       */
      async testConnection(): Promise<boolean> {
        if (this.storageType === 'oss') {
          return await ossService.testConnection();
        } else {
          // 测试本地存储
          const testDir = path.join(process.cwd(), 'uploads');
          try {
            if (!fs.existsSync(testDir)) {
              fs.mkdirSync(testDir, { recursive: true });
            }
            const testFile = path.join(testDir, '.test');
            fs.writeFileSync(testFile, 'test');
            fs.unlinkSync(testFile);
            console.log('[Storage] 本地存储测试成功');
            return true;
          } catch (error) {
            console.error('[Storage] 本地存储测试失败:', error);
            return false;
          }
        }
      }
    }

    // 导出单例
    export const storageService = new StorageService();
    export default storageService;