File size: 25,776 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

// ---------------------------------------------------------------
// Local CDN Integration
//
//  A mock integeration that stores files in a local directory
//  rather than using a CDN
//
// ---------------------------------------------------------------

import { type IntegrationsManager } from 'omni-shared';
import {
  CdnIntegration,
  CdnResource,
  type ICdnTicket,
  type ICdnIntegrationConfig,
  type ICdnResource,
  type ICDNFidServeOpts,
  EOmniFileTypes
} from './CdnIntegration.js';
import mime from 'mime-types';
import fs from 'fs/promises';
import { createReadStream } from 'fs';
import { ensureDir } from 'fs-extra';
import detectContentType from 'detect-content-type';
import { fileTypeFromBuffer } from 'file-type';
import { performance } from 'perf_hooks';
import sharp from 'sharp';
import { Readable, PassThrough } from 'stream';
import murmurHash from 'imurmurhash';

import { customAlphabet } from 'nanoid';
import path from 'path';
import { file as tmpFile } from 'tmp-promise';
import type MercsServer from 'core/Server.js';

const HARD_DELETE_AFTER_MINUTES = 60 * 24 * 7; // 7 days
const THUMBNAIL_RETENTION = '7d';

const MIN_SIZE = 32; // Set your min size
const MAX_SIZE = 512; // Set your max size

// These are the allowed fit options in sharp
const ALLOWED_FIT_OPTIONS = ['cover', 'contain', 'fill', 'inside', 'outside'];
// These are the allowed position options in sharp
const ALLOWED_POSITION_OPTIONS = [
  'center',
  'north',
  'northeast',
  'east',
  'southeast',
  'south',
  'southwest',
  'west',
  'northwest',
  'entropy',
  'attention'
];

// FID Logic for LocalCDN
const fidRegex = /[^a-z0-9,]/g;
const NANOID_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
const fidGenerator = customAlphabet(NANOID_ALPHABET, 10);

class CdnObjectNotFoundError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'CDN_NOT_FOUND';
  }
}

interface ILocalCdnIntegrationConfigDetails extends ICdnIntegrationConfig {
  root: string; // the directory root where we store things
  url: string; // the url to the root directory
  default_ttl: string;
}

interface ILocalCdnIntegrationConfig extends ICdnIntegrationConfig {
  local: ILocalCdnIntegrationConfigDetails;
}

class LocalCdnIntegration extends CdnIntegration {
  missCache: Set<string>;

  constructor(id: string, manager: IntegrationsManager, config: ILocalCdnIntegrationConfig) {
    super(id, manager, config || {});

    this.missCache = new Set<string>();

    if (this.detailConfig.root == null || this.detailConfig.url == null) {
      throw new Error('Local CDN Integration requires a root and url set in the configuration!');
    }
  }

  get detailConfig(): ILocalCdnIntegrationConfigDetails {
    return (this.config as ILocalCdnIntegrationConfig).local;
  }

  async load(): Promise<boolean> {
    this.info('CDN is LOCAL', this.detailConfig);
    return await super.load();
  }

  async writeObject(key: string, data: Buffer) {
    const file = this.getPathForFid(key);
    this.debug('writeObject()', key, file);
    await fs.writeFile(file, data);
    return await fs.stat(file);
  }

  async hasFile(key: string) {
    const file = this.getPathForFid(key);
    this.info('hasFile', key, file);
    try {
      const stats = await fs.stat(file);
      if (stats.isFile()) {
        this.info('hasFile true');
        return true;
      }
    } catch {
      this.verbose('hasFile()', 'file not found', key, file);
    }
    this.info('hasFile false');
    this.verbose('hasFile()', 'isFile false', key, file);
    return false;
  }

  async readObject(key: string) {
    const file = this.getPathForFid(key);
    this.debug('readObject()', key, file);

    // check if the file exists with stat, if not, call kvStorage.del(key)
    if (await this.hasFile(key)) {
      return await fs.readFile(file);
    }
    //await this.softDelete(key)
    this.kvStorage.del(`file.${key}`);
    throw new CdnObjectNotFoundError(`cdn.readObject(): no record found for ${key}`);
  }

  async deleteObject(key: string) {
    const file = this.getPathForFid(key);
    const stat = await fs.stat(file);
    if (stat && stat.isFile()) {
      this.verbose(`Purging expired file ${key} from local CDN`);
      this.debug('deleteObject()', key, file);
      try {
        await fs.unlink(file);
        return true;
      } catch (ex) {
        this.warn(`Error Purging expired file ${key} from local CDN`, ex);
        return false;
      }
    }
    return false;
  }

  getPathForFid(fid: string) {
    fid = fid.replace(fidRegex, ''); // sanitize
    const [volume, file] = fid.split(','); // split into volume and file
    const fileName = `${this.detailConfig.root}/${volume}/${file}`;
    this.verbose('getPathForFid', fid, fileName);
    return fileName;
  }

  // This fires whenever the kvstore expires a ew
  override async onExpired(purgedKeys: string[] = []): Promise<any> {
    this.info(`Purging ${purgedKeys.length} expired files from local CDN`);
    for (const key of purgedKeys) {
      if (key.startsWith('file.')) {
        const fid = key.substring(5);
        try {
          await this.deleteObject(fid);
        } catch (ex) {
          this.verbose(`Error Purging expired file ${fid} from local CDN`, ex);
        }
      }
    }
  }

  // returns a ticket for a file
  async assign(opts: { ttl?: string }): Promise<ICdnTicket | null> {
    opts ??= {};
    const volumeId = Math.floor(Math.random() * 99)
      .toString()
      .padStart(2, '0');
    /*const fileId = Math.floor(Math.random() * (0xffffffffff + 1))
      .toString(16)
      .padStart(10, '0');
    */
    const fileId = fidGenerator(10).padStart(10, '0');

    await ensureDir(`${this.detailConfig.root}/${volumeId}`);

    const ret = {
      fid: `${volumeId},${fileId}`,
      count: 1,
      url: this.detailConfig.url,
      publicUrl: this.detailConfig.url
    };

    return ret;
  }

  // sets the expiry for a file
  async setExpiry(file: CdnResource, userId: string, expiry: undefined | number) {
    if (file && file.fid) {
      if (expiry != null) {
        this.kvStorage.setExpiry(`file.${file.fid}`, expiry);
        file.expires = expiry;
      } else {
        this.kvStorage.setExpiry(`file.${file.fid}`, null);
        file.expires = Number.MAX_SAFE_INTEGER;
      }
      return this.updateFileEntry(file);
    }
  }
  async softDelete(fid: string): Promise<any> {
    // TODO: For proper multi user support, we need to gate delete operations by userId:
    // You shouldn't be able to delete an object you don't own, even if you know it's ID

    if (!fid || typeof fid !== 'string' || fid.length < 10) {
      this.warn('Softdelete Invalid fid', fid);
      return false;
    }

    // remove everything that's not hex or comma to avoid any kind of injection
    fid = fid.replace(fidRegex, '');

    const obj = this.kvStorage.get(`file.${fid}`);
    if (obj != null) {
      this.info('Soft deleting', fid);
      // create a tracking entry
      const hard_delete_after_ms = HARD_DELETE_AFTER_MINUTES * 1000 * 60;
      this.kvStorage.softDelete(`file.${fid}`, obj.expiry ?? Date.now() + hard_delete_after_ms);

      return true;
    } else {
      return false;
    }
  }

  guessIfBase64(str: string): boolean {
    if (!str || str.length & 3) {
      return false;
    }

    const mainBodyPattern = /^[A-Za-z0-9+/]+$/; // Only characters from the base64 alphabet
    const suffixPattern = /^(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})?$/; // ... or with Padding

    const mainBody = str.substring(0, str.length - 4);
    const suffix = str.substring(str.length - 4);
    return mainBodyPattern.test(mainBody) && suffixPattern.test(suffix);
  }

  // Writes a file to storage using the assigned ticket
  // It will try to infer the mime type and extension and alter the ticket fid to amend inferred extension if needed
  async write(
    record: { data: Buffer | string },
    ticket: { fid: string; url: string; publicUrl: string },
    opts: {
      ttl?: string;
      mimeType?: string;
      fileName?: string;
      userId?: string;
      jobId?: string;
      tags?: string[];
      fileType?: EOmniFileTypes;
    },
    meta?: any
  ): Promise<CdnResource> {
    this.info('cdn.write()', 'writing file', ticket, opts);

    // dereference metadata
    meta = JSON.parse(JSON.stringify(meta || {})) ?? {};

    // @ts-ignore
    if (record.data.url && !record.data.ticket) {
      // @ts-ignore
      record.data = record.data.url;
    }

    let encoding = 'binary';

    // When we receive strings, it's a bit tricky to figure out what to do
    if (typeof record.data === 'string') {
      // let's start assuming base64
      encoding = 'base64';
      // strip header if needed
      if (opts.mimeType?.startsWith('text/')) {
        opts.fileType ??= EOmniFileTypes.document;
        // unless it's text, then we assume utf8
        encoding = 'utf8';
      } else if (record.data.indexOf('data:') === 0) {
        // seems like a data url, let's strip the header
        record.data = record.data.split(',')[1];
      } else if (
        // seems like a  url, use our interfal fetch to get it
        // Note: this does act as a CORS proxy
        record.data.indexOf('http') === 0 ||
        record.data.indexOf('/fid/') === 0
      ) {
        const resp = await CdnIntegration.fetch(record.data);
        record.data = resp.data;
        // it's a cdn object, get it
      } else if (record.data.indexOf('fid://') === 0) {
        const fid = record.data.split('://')[1];
        this.success('cdn:write()', 'found fid, returning', fid);
        return await this.getByFid(fid);
      }
      // let's try some heuristics
      else if (this.guessIfBase64(record.data)) {
        this.warn('Someone passed a base64 encoded image without header into cdn.write(), so we guessed...');
        // record.data = record.data
      }
      // everything else is probably a text file?
      else {
        encoding = 'utf8';
        opts.mimeType ??= 'text/plain';
        opts.fileType ??= EOmniFileTypes.document;
      }

      // @ts-ignore
      record.data = Buffer.from(record.data, encoding);
    }

    if (!record.data) {
      throw new Error('cdn.write(): no data supplied');
    }

    // ---------------------- Sanitize the metadata filename ---------------------------------
    let extName;
    let mimeType = opts.mimeType;
    let fileName = opts.fileName;

    // If we're dealing with text, let's augment the meta-data a bit
    if (encoding === 'utf8' && mimeType?.startsWith('text/')) {
      if (mimeType.startsWith('text/markdown')) {
        // Try to infer reasonable filenames
        fileName =
          fileName ??
          record.data
            .toString()
            .substring(0, 20)
            .trim()
            .replace(/[^a-z0-9]/gi, '_');
        extName = 'md';
      } else if (mimeType.startsWith('text/html')) {
        extName = 'html';
      } else if (mimeType.startsWith('text/svg')) {
        extName = 'html';
      } else {
        // Try to infer reasonable filenames
        fileName =
          fileName ??
          record.data
            .toString()
            .substring(0, 20)
            .trim()
            .replace(/[^a-z0-9]/gi, '_');
        extName = 'txt';
      }
    } else {
      // For non text types, we try various ways of identifying the mime type
      if (!mimeType || !mimeType.startsWith('text/')) {
        const t = await fileTypeFromBuffer(record.data);
        if (t != null) {
          mimeType = t.mime ?? mimeType;
          extName = t.ext;
        } else {
          mimeType = detectContentType(record.data);
        }
      }
    }

    const sanitizedFilename = this.mangleFilename(
      fileName ?? ticket.fid,
      // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
      extName || mime.extension(mimeType) || undefined
    );

    // ---------------------- Generate the on disk cdn object filename  ----------------------

    const fileType = opts.fileType ?? CdnResource.determineFileTypeFromMimeType(mimeType) ?? EOmniFileTypes.file;
    let nsfw:
      | {
          isNsfw?: boolean;
          Sexy?: number;
          Porn?: number;
          Hentai?: number;
          Drawing?: number;
          Neutral?: number;
        }
      | undefined;

    if (fileType === EOmniFileTypes.image) {
      try {
        //@ts-ignore
        if (this.app.options.uncensored) {
          meta.nsfw = {
            status: 'disabled',
            reason: '--uncensored option activated'
          };
        } else {
          //@ts-ignore
          const result = await this.app.nsfwCheck(record.data, { maxDimensions: 299 });
          nsfw = { ...result.classes, isNsfw: result.isNsfw, status: 'success' };
          meta.nsfw = nsfw;
        }
      } catch (err: unknown) {
        this.error('nsfwCheck failed', err);
        meta.nsfw = {
          status: 'failed',
          reason: (err as any)?.message
        };
      }
    }

    const result = await this.writeObject(ticket.fid, record.data);
    let ttl: number | undefined = this.parseTTL(opts.ttl ?? '');

    const expiresAt = ttl > 0 ? ttl + Date.now() : Number.MAX_SAFE_INTEGER; // permanent

    const file = new CdnResource({
      fid: ticket.fid,
      ticket,
      fileType,
      expires: expiresAt, // permanent
      fileName: sanitizedFilename,
      size: result.size,
      url: this.getCdnUrl(ticket),
      furl: '',
      mimeType,
      meta
    });
    if (file.isImage()) {
      file.meta = Object.assign(file.meta, CdnResource.getImageMeta(record.data));
    }

    if (ttl > 0) {
      /* empty */
    } else {
      ttl = undefined;
    }

    let tags: string[] | undefined;
    if (opts.tags && Array.isArray(opts.tags) && opts.tags.length > 0) {
      tags = opts.tags.map((t: string) => `tag.${t}`);
    }

    // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
    if ( opts.jobId) {
      tags ??= [];
      tags = tags.concat([`job.${opts.jobId}`]);
    }

    this.kvStorage.set(
      `file.${ticket.fid}`,
      file,
      ttl ? ttl + Date.now() : undefined,
      tags,
      opts.userId?.trim().toLowerCase()
    );

    // Remove the fid from the missCache since it now exists
    this.missCache.delete(ticket.fid);

    this.verbose('cdn.write()', result); // {"size": 1234}

    if (opts.userId) {
      const server = this.app as MercsServer;
      await server.sendToastToUser(opts.userId, {
        message: `File created: ${file.fileName}`,
        options: { type: 'info' }
      });
    }

    return file;
  }

  async updateFileEntry(file: ICdnResource): Promise<ICdnResource> {
    if (file) {
      this.kvStorage.updateValue(`file.${file.fid}`, file);
    }
    return await Promise.resolve(file);
  }

  async importLocalFile(filePath: string, tags: string[] = [], userId?: string): Promise<ICdnResource | null> {
    const buffer = await fs.readFile(filePath);
    const inputString = buffer.toString('binary'); // Convert buffer to binary string
    const hash = murmurHash(inputString + filePath)
      .result()
      .toString();

    const existsFid = this.kvStorage.get(`sample-import.${hash}`);
    if (existsFid) {
      this.info('Sample with hash', hash, 'already exists');
      //return await this.find(existsFid)
      return null;
    } else {
      this.info('Importing sample with hash ', hash, '...');
      const result = await this.put(buffer, { fileName: path.basename(filePath), tags, userId });
      this.kvStorage.set(`sample-import.${hash}`, result.fid);
      return result;
    }
  }

  async put(
    data: Buffer | string,
    opts?: {
      ttl?: string;
      mimeType?: string;
      fileName?: string;
      userId?: string;
      jobId?: string;
      tags?: string[];
      fileType?: EOmniFileTypes;
    },
    meta?: any
  ): Promise<CdnResource> {
    opts ??= {};
    const ticket: ICdnTicket | null = await this.assign(opts);
    if (ticket != null) {
      const result = await this.write({ data }, ticket, opts, meta);
      this.success('put()', result, result.ticket);
      // @ts-nocheck

      return result;
    }

    throw new Error('cdn.put(): no Ticket supplied');
  }

  // Store a file for a temporary period of time
  async putTemp(
    data: Buffer | string,
    opts?: {
      ttl?: string;
      mimeType?: string;
      fileName?: string;
      userId?: string;
      jobId?: string;
      tags?: string[];
      fileType?: EOmniFileTypes;
    },
    meta?: any
  ): Promise<CdnResource> {
    opts ??= {};
    opts.ttl ??= this.detailConfig.default_ttl;
    this.info('cdn.putTemp()', opts, meta, this.detailConfig);
    return await this.put(data, opts, meta);
  }

  // returns the volume server for a specific fid
  async find(fid: string, userId?: string): Promise<CdnResource | null> {
    // Currently the CDN will happily serve any file to anyone as long as they know a valid fid
    // In the future we may want to gate access by user session, which could be done here by
    // validating the userId against the tags of the raw entry
    // TODO: Slice userId to perform access check
    if (fid == null || fid.length == 0)
    {
      throw new Error("Null file identifier passed to cdn.find")
    }
    // Allow
    if (fid.startsWith('sample-import:'))
    {
      const actualFid = this.kvStorage.get(fid.replace('sample-import:', 'sample-import.'))
      console.warn("looking for static file",fid, actualFid)
      if (actualFid != null)
      {
        fid = actualFid
      }
    }

    const ret = await Promise.resolve(this.kvStorage.get(`file.${fid}`, true));
    const res = this.kvStorage._getRowValue(ret)
    if (res) {
      res.fid ??= fid;
      res.expires = res.expiry
    }
    const resource = new CdnResource(res)
    return resource;
  }

  async getByFid(fid: string, opts?: any, format?: 'asBase64' | 'stream' | 'file'): Promise<CdnResource> {
    return await this.get({ fid }, opts, format);
  }

  async get(
    ticket: Partial<ICdnTicket>,
    opts?: object,
    format?: 'asBase64' | 'base64' | 'stream' | 'file'
  ): Promise<CdnResource> {
    // @ts-expect-error
    if (ticket instanceof CdnResource || ticket.ticket) {
      // @ts-expect-error
      ticket = ticket.ticket;
    }
    let fid = ticket.fid;
    if (!fid) {
      const error = new CdnObjectNotFoundError(`cdn.get(): no record found for ${ticket.fid}`);
      throw error;
    }

    // First retrieve metadata from kv storage
    const cdnRecord = await this.find(fid);
    if (cdnRecord === null) {
      const error = new CdnObjectNotFoundError(`cdn.get(): no record found for ${ticket.fid}`);
      throw error;
    }

    // Fid may have been translated, so we have to update here
    fid = cdnRecord.fid

    // If we're asked for a stream, get it
    if (format === 'stream') {
      const fileLocation = this.getPathForFid(fid);
      if (await this.hasFile(fid)) {
        cdnRecord.data = createReadStream(fileLocation).on('error', (err) => {
          this.error('get() failed with error', err);
        });
        return cdnRecord;
      } else {
        this.kvStorage.del(`file.${ticket.fid}`);
        const error = new CdnObjectNotFoundError(`cdn.get(): no record found for ${ticket.fid}`);
        throw error;
      }
    } else if (format === 'file') {
      cdnRecord.data = await tmpFile(); // const { path, fd, cleanup } =
      await fs.copyFile(this.getPathForFid(fid), cdnRecord.data.path);

      return cdnRecord;
    }

    const data = await this.readObject(fid);
    if (format === 'base64' || format === 'asBase64') {
      cdnRecord.data = data.toString('base64');
    } else {
      cdnRecord.data = data;
    }

    // Patch for legacy files
    if (!cdnRecord.fid) {
      cdnRecord.fid = fid;
    }

    return cdnRecord;
  }

  async checkFileExists(fid: string): Promise<boolean> {
    const record = await this.find(fid)
    return record != null;
  }

  async serveFile(fid: string, opts: ICDNFidServeOpts, reply: any): Promise<any> {
    const start = performance.now();
    opts ??= {};
    const result = this.serveFileInternal(fid, opts, reply);
    const statusCode = reply?.statusCode ?? 'unknownStatusCode';
    this.debug(`CDN: ${fid} ${statusCode} took ${Math.max(1, performance.now() - start).toFixed()} ms`);

    return await result;
  }

  async serveFileResponse(download: boolean, file: any, dataStream: PassThrough | Readable, reply: any) {
    if (download) {
      return reply
        .header('Content-Description', 'File Transfer')
        .header('Content-Length', file.size)
        .header('Content-Type', 'application/octet-stream')
        .header('Content-Disposition', `attachment; filename="${file.fileName}"`)
        .header('Content-Transfer-Encoding', 'binary')
        .send(dataStream);
    } else {
      return reply
        .header('Content-Length', file.size)
        .header('Content-type', file.mimeType)
        .header('Content-Disposition', `inline; filename="${file.fileName}"`)
        .header('Content-Transfer-Encoding', 'binary')
        .header(
          'Cache-Control',
          `public, max-age=${file.expires ? Math.max(0, file.expires - Date.now() / 1000) : 24 * 60 * 60}, immutable`
        )
        .send(dataStream);
    }
  }

  async serveFileInternal(fid: string, opts: ICDNFidServeOpts, reply: any): Promise<any> {
    // Strip potential extension
    const lastDotIndex = fid.lastIndexOf('.');
    if (lastDotIndex !== -1) {
      fid = fid.substring(0, lastDotIndex);
    }

    if (this.missCache.has(fid)) {
      return this.fileNotFoundReply(reply, 'missCache');
    }

    const hash = murmurHash(JSON.stringify(opts)).result().toString();
    const thumbnailKey = `thumb.${fid}.${hash}`;

    // Attempt to get the cached thumbnail
    const cachedThumbnail = this.kvStorage.get(thumbnailKey);
    if (cachedThumbnail) {
      this.verbose('CDN: thumbnail from cache', thumbnailKey);
      // If the thumbnail exists in cache, serve it directly
      return reply.send(cachedThumbnail);
    }

    try {
      const file = await this.get({ fid }, {}, 'stream');

      if (file == null || file.data == null) {
        this.missCache.add(fid);
        return this.fileNotFoundReply(reply, `${file == null ? 'file is null' : 'no data'}`);
      }

      const dataStream: Readable = file.data as Readable;

      const pass = new PassThrough();

      // validate and sanitize opts

      const download = (opts.download as any) === 'true' || opts.download === true;

      if (!file.isImage()) {
        return await this.serveFileResponse(download, file, dataStream, reply);
      }

      let width = parseInt(opts.width?.toString() ?? '');
      let height = parseInt(opts.height?.toString() ?? '');
      const fit = opts.fit;
      const position = opts.position;

      // If only width or height is provided, assume square
      if (isNaN(width) && !isNaN(height)) {
        width = height;
      } else if (!isNaN(width) && isNaN(height)) {
        height = width;
      }

      // Validate width, height, fit, and position options
      if (width > 0 && height > 0) {
        if (
          width % 32 === 0 &&
          height % 32 === 0 &&
          width >= MIN_SIZE &&
          width <= MAX_SIZE &&
          height >= MIN_SIZE &&
          height <= MAX_SIZE &&
          (fit === undefined || ALLOWED_FIT_OPTIONS.includes(fit)) &&
          (position === undefined || ALLOWED_POSITION_OPTIONS.includes(position))
        ) {
          let transform = sharp().resize({
            width,
            height,
            fit,
            position,
            fastShrinkOnLoad: true
          });

          // if the file is flagged nsfw, blur it
          //@ts-ignore
          if (file.meta.nsfw?.isNsfw && !this.app.options.uncensored) {
            transform = transform.blur(20);
          }

          dataStream.pipe(transform).pipe(pass);

          const chunks: Buffer[] = [];

          return await new Promise((resolve, reject) => {
            pass
              .on('data', (chunk) => chunks.push(chunk))
              .on('end', () => {
                this.info('CDN: thumbnail generated', thumbnailKey);
                const thumbnailBuffer = Buffer.concat(chunks);
                const ttl = Math.max(
                  1,
                  Math.min(
                    this.parseTTL(THUMBNAIL_RETENTION),
                    file.expires ? file.expires - Date.now() : this.parseTTL(THUMBNAIL_RETENTION)
                  )
                );

                this.kvStorage.set(thumbnailKey, thumbnailBuffer, ttl > 0 ? ttl + Date.now() : undefined);
                file.size = thumbnailBuffer.length;
                resolve(this.serveFileResponse(download, file, Readable.from(thumbnailBuffer), reply));
              });
          });
        } else {
          return reply.status(422).send({ error: 'Invalid resize options provided.' + JSON.stringify(opts) });
        }
      }
      return await this.serveFileResponse(download, file, dataStream, reply);
    } catch (ex: any) {
      this.missCache.add(fid);
      omnilog.error(ex);
      if (ex instanceof CdnObjectNotFoundError) {
        return this.fileNotFoundReply(reply, ex.message);
      }
      return reply.status(500).send({ error: 'Internal server error' }); // Catch all internal server error.
    }
  }

  fileNotFoundReply(reply: any, reason: string) {
    return reply
      .status(410)
      .header('Cache-Control', 'no-cache, no-store, must-revalidate')
      .send({ exists: false, reason });
  }
}

export { LocalCdnIntegration, type ILocalCdnIntegrationConfig };