Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

19.1 KB
Raw Blame
import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { logDebug } from '../audit/audit-log.logger';
import { contentTypeFor } from './content-type';
import type { ReplicaFailure } from './drivers/mirror.driver';
import { decideRange, isNotModified, validatorsFor, type ServingHeaders, type Validators } from './http-serving';
import { assertValidKey } from './storage-keys';
import { StorageRegistryService } from './storage-registry.service';
import {
  isClientAbortError,
  StorageNotFoundError,
  type ByteRange,
  type ListOptions,
  type LocalTempFile,
  type ObjectStat,
  type PutOptions,
  type ServedCategory,
  type StorageDriver,
} from './storage.types';
import { Injectable } from '@nestjs/common';

import type { Response } from 'express';
import { randomUUID } from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import type { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';

export interface SendOptions {
  contentType?: string;
  disposition?: string;
}

interface Resolved {
  driver: StorageDriver;
  keyPrefix: string;
  key: string;
}

/**
 * The folder under `files` the assistant keeps the files attached to a
 * conversation in (assistant-attachments.service.ts takes it from here). They
 * live by a retention of days, so no demo baseline can rely on their bytes, and
 * a demo instance deletes them as any other does: attaching is not refused to
 * the demo account, and kept bytes of a public visitor's files would stay for good.
 */
export const ASSISTANT_FILES_PREFIX = 'assistant/';

/**
 * What a delete does on a demo instance, per category: `true` leaves the
 * object where it is. The hourly demo reset (src/demo/demo-reset.ts) puts only
 * the database back to its baseline, so a file a visitor deleted in between
 * would be missing from the restored rows for good. The categories holding
 * content that rows point at are kept; the caches and the backups are deleted
 * as on any other instance. A Record, so a new category does not compile until
 * it is decided here.
 */
const KEPT_ON_DEMO: Readonly<Record<ServedCategory, boolean>> = {
  // Trip files, collab note files and chat images. The assistant's attachments
  // under ASSISTANT_FILES_PREFIX are the exception, see keepsOnDelete.
  files: true,
  // Journey photos, videos, posters, covers and thumbs. A thumb a row records
  // is served as it is and never made again, so it stays with its photo.
  journey: true,
  // Trip and collection covers.
  covers: true,
  avatars: true,
  // Images uploaded for places.
  places: true,
  // The legacy shared photo folder, served and backed up like the uploads.
  photos: true,
  // A cache of the photos the map providers hand back, written by every lookup
  // and swept nightly. Kept, it would grow without bound on a public demo.
  'photos-google': false,
  // A cache of Immich and Synology assets that lives an hour and is fetched again on a miss.
  'photos-trek': false,
  // Never part of the baseline: the admin deletes them and auto-backup rotates them.
  backups: false,
};

/**
 * The facade all call sites use — and the ONLY entry point: callers address
 * storage exclusively as (category, name) on this injected service. They
 * never import a driver, never hold a driver or backend reference, and never
 * know which instance serves a category — resolution happens inside the
 * registry on every call. That is what makes backend reconfiguration a
 * config-plus-reload() operation instead of a code change.
 *
 * `sendToResponse` and `withLocalFile` are the two helpers that make a remote
 * backend a driver-only change later: every byte-consumer goes through one of
 * them, and each owns the local-fast-path vs stream branch exactly once.
 */
@Injectable()
export class StorageService {
  constructor(
    private readonly registry: StorageRegistryService,
    // Whether DEMO_MODE is on, read on every delete. A hand-built facade (the
    // unit suites) reads the same live env the container's provider does.
    private readonly runtimeEnv: RuntimeEnvService = new RuntimeEnvService(),
  ) {}

  /** Driver-agnostic global scratch space (data/tmp). */
  tempDir(): string {
    return this.registry.tempDir();
  }

  /** The category backend's same-volume spool dir, else tempDir(). Resolved per call. */
  spoolDirFor(category: ServedCategory): string {
    return this.registry.resolve(category).driver.getSpoolDir?.() ?? this.tempDir();
  }

  // The read/write methods are async so key-validation failures surface as
  // rejections, never synchronous throws — callers treat storage uniformly.

  async put(
    category: ServedCategory,
    name: string,
    source: Readable | LocalTempFile,
    opts?: PutOptions,
  ): Promise<void> {
    const { driver, key } = this.resolve(category, name);
    return driver.put(key, source, opts);
  }

  async getStream(
    category: ServedCategory,
    name: string,
    range?: ByteRange,
  ): Promise<{ stream: Readable; stat: ObjectStat }> {
    const { driver, key } = this.resolve(category, name);
    return driver.getStream(key, range);
  }

  async stat(category: ServedCategory, name: string): Promise<ObjectStat | null> {
    const { driver, key } = this.resolve(category, name);
    return driver.stat(key);
  }

  async exists(category: ServedCategory, name: string): Promise<boolean> {
    return (await this.stat(category, name)) !== null;
  }

  /**
   * Whether delete() leaves this object where it is: on a demo instance, one of
   * a category KEPT_ON_DEMO keeps, the assistant's attachments excepted. A
   * sweep asks first, so it never counts as reclaimed an object that stays.
   */
  keepsOnDelete(category: ServedCategory, name: string): boolean {
    if (!KEPT_ON_DEMO[category]) return false;
    if (category === 'files' && name.startsWith(ASSISTANT_FILES_PREFIX)) return false;
    return this.runtimeEnv.isDemoMode();
  }

  /**
   * Idempotent, like the drivers. On a demo instance an object keepsOnDelete()
   * names is left alone and the call resolves as if it were gone; a later put()
   * under the same name still replaces it. That holds for the bytes a request
   * takes back out after a refused upload too: on a demo they stay, as a
   * visitor's uploads already do once the hourly reset drops their rows. The
   * name is validated first in both modes, so a hostile one is refused either way.
   */
  async delete(category: ServedCategory, name: string): Promise<void> {
    const { driver, key } = this.resolve(category, name);
    if (this.keepsOnDelete(category, name)) {
      // The category only: the name can come from a request, and a log line is no place for it.
      logDebug(`[storage] demo mode keeps a deleted ${category} object, the hourly reset restores rows that name it`);
      return;
    }
    return driver.delete(key);
  }

  /** Names in and out are category-relative — the key prefix stays a registry detail. */
  async *list(category: ServedCategory, subPrefix = '', opts?: ListOptions): AsyncIterable<ObjectStat> {
    const { driver, keyPrefix } = this.registry.resolve(category);
    for await (const stat of driver.list(keyPrefix + subPrefix, opts)) {
      yield { ...stat, key: stat.key.slice(keyPrefix.length) };
    }
  }

  /**
   * Serve an object into an Express response. Local backends use the
   * root-relative res.sendFile form — absolute paths resolve against the
   * rewritten req.url under the ExpressAdapter and spuriously 404
   * (files-download.controller.ts) — which brings etag/range/conditional-GET
   * for free. Path-less (remote) drivers, and a local path whose file has
   * vanished from disk (checked via `localPathIfPresent`, never trusted from
   * `getLocalPath` alone), both fall through to the stream branch — for a
   * MirrorDriver that reaches a replica that still holds the object, for a
   * plain LocalDriver a genuine miss (`sendStreamed`'s pre-flight stat throws
   * StorageNotFoundError, same as always — one extra driver call). A miss
   * throws StorageNotFoundError: the caller owns the route's miss contract
   * (404 envelope, 204-empty, or next()).
   */
  async sendToResponse(category: ServedCategory, name: string, res: Response, opts?: SendOptions): Promise<void> {
    const { driver, key } = this.resolve(category, name);

    const localPath = this.localPathIfPresent(driver, key);
    if (localPath !== null) {
      this.applyHeaders(res, opts);
      await new Promise<void>((resolve, reject) => {
        res.sendFile(path.basename(localPath), { root: path.dirname(localPath) }, (err) => {
          if (!err) {
            resolve();
            return;
          }
          // Client gone / bytes already on the wire: nothing useful to send —
          // mirrors res.sendFile's own default callback (express
          // response.js), which is the exact contract
          // storageStaticHandler (platform.routes.ts) already implements
          // caller-side for the /uploads/* static mounts.
          if (isClientAbortError(err)) {
            resolve();
            return;
          }
          reject(err);
        });
      });
      return;
    }

    await this.sendStreamed(driver, key, res, opts);
  }

  /**
   * The proxying serve — every path-less backend, plus a local primary whose
   * file has vanished. `res.sendFile` gives the local branch content-type
   * sniffing, ETag/Last-Modified, conditional GET, Range and HEAD for free;
   * nothing gives them to a stream, so this reproduces the same contract from
   * ONE pre-flight `stat()` plus the request headers (decisions live in the
   * pure http-serving.ts).
   *
   * That stat is what makes the rest possible and is deliberately paid on
   * every request: it turns a miss into a throw before a single header is
   * set (the caller's 404/204/next() contract), and one HeadObject then
   * answers 304s, 416s, HEADs and suffix ranges without ever opening a body.
   * It is also the single source of truth for the range math — the ranged
   * `getStream` response is never re-consulted for the total size.
   */
  private async sendStreamed(driver: StorageDriver, key: string, res: Response, opts?: SendOptions): Promise<void> {
    const stat = await driver.stat(key);
    if (stat === null) throw new StorageNotFoundError(key);
    // Express always populates res.req — the local branch's res.sendFile
    // already depends on it — so serving needs no caller churn.
    const req = res.req;
    const headers = req.headers as ServingHeaders;
    const validators = validatorsFor(stat);

    if (isNotModified(headers, validators)) {
      // 304 carries validators and nothing else: no body, no Content-Length,
      // and above all no getStream — the whole point of revalidating.
      this.applyValidators(res, validators);
      res.statusCode = 304;
      res.end();
      return;
    }

    const decision = decideRange(headers, stat.size, validators.etag);
    if (decision.kind === 'unsatisfiable') {
      res.statusCode = 416;
      res.setHeader('Content-Range', `bytes */${stat.size}`);
      res.end();
      return;
    }

    const range = decision.kind === 'partial' ? { start: decision.start, end: decision.end } : undefined;

    // HEAD is answered entirely from the stat — fetching a body just to
    // discard it would be one wasted round trip per request.
    if (req.method === 'HEAD') {
      this.stageServeHeaders(res, key, opts, validators, stat.size, range);
      res.end();
      return;
    }

    // The body opens BEFORE a single header is staged. getStream can still
    // fail after a successful stat — a delete that raced it, a backend blip —
    // and a caller that recovers on the SAME response object must find it
    // pristine: trek-photo-cache.service.ts treats a pre-header
    // StorageNotFoundError as a cache miss and photo-resolver.service.ts then
    // re-serves through that very res. A 206 status and a Content-Range left
    // behind by an attempt that never produced a byte would corrupt that
    // retry.
    const { stream } = await driver.getStream(key, range);
    try {
      this.stageServeHeaders(res, key, opts, validators, stat.size, range);
    } catch (err) {
      // The body is already open (an S3 keep-alive socket, an fd) and nothing
      // has piped it yet, so nothing else ever will — destroy it before the
      // throw escapes. Staging really can fail: res.setHeader raises
      // ERR_INVALID_CHAR on a control character, and Content-Disposition is
      // built from client-supplied filenames (files-download.controller.ts
      // uses multer's originalname), so a name with a CR/LF reaches here.
      stream.destroy();
      throw err;
    }
    try {
      // pipeline (unlike the hand-rolled pipe+Promise it replaces) destroys
      // BOTH ends on any failure, so a client abort no longer leaks the
      // source stream (an S3 keep-alive socket, an open fd, …).
      await pipeline(stream, res);
    } catch (err) {
      // Gate the swallow on headersSent, not just the error code: the source
      // here is the driver's raw stream (e.g. the S3 SDK's HTTP response),
      // which can itself throw ECONNRESET/ERR_STREAM_PREMATURE_CLOSE on a
      // genuine network blip that has nothing to do with the browser client
      // — before any byte has reached res. A pre-header failure of ANY code
      // must still reject so the caller's miss contract (404/204/next())
      // runs; only a POST-header failure is the client walking away
      // mid-download, and only then is an abort-like code safe to swallow.
      if (!res.headersSent || !isClientAbortError(err)) throw err;
    }
  }

  /**
   * Hand a consumer (Jimp, zip packing, Immich push, …) a real filesystem
   * path: the local fast-path when the driver has one AND the file is
   * actually on disk (`localPathIfPresent`), else download to tempDir() and
   * clean up afterwards — the same fall-through `sendToResponse` uses, and
   * exactly the branch a remote driver (or a local path gone missing) will
   * exercise.
   */
  async withLocalFile<T>(category: ServedCategory, name: string, fn: (absPath: string) => Promise<T>): Promise<T> {
    const { driver, key } = this.resolve(category, name);

    const localPath = this.localPathIfPresent(driver, key);
    if (localPath !== null) {
      return fn(localPath);
    }

    const tmp = path.join(this.tempDir(), randomUUID());
    try {
      const { stream } = await driver.getStream(key);
      // Inside the try, so a download cut short does not leave its part behind.
      await pipeline(stream, fs.createWriteStream(tmp));
      return await fn(tmp);
    } finally {
      await fs.promises.rm(tmp, { force: true });
    }
  }

  /**
   * A narrow locality probe: the real filesystem path for the object, or
   * null when there isn't one — a remote (path-less) driver, or a local
   * driver whose file no longer exists on disk. The fs-existence check is
   * the fail-safe half of the contract: a caller that trusts a stale path
   * (deleted between listing and this call, or by another process) would
   * otherwise silently skip or corrupt-read the object instead of falling
   * back to the stream path. Same spirit as spoolDirFor — exposes a path,
   * never a driver.
   */
  async getLocalPathOrNull(category: ServedCategory, name: string): Promise<string | null> {
    const { driver, key } = this.resolve(category, name);
    return this.localPathIfPresent(driver, key);
  }

  /** Replica failures from mirror backends — logged there, surfaced here. */
  health(): { replicaFailures: readonly ReplicaFailure[] } {
    return { replicaFailures: this.registry.replicaFailures() };
  }

  /**
   * Re-read storage config from `app_settings` and swap the registry's
   * resolved backends/categories — the narrow passthrough a restore uses
   * after reopening the DB (backup.impl.ts's restoreFromZip), so rehydrated
   * bytes land where the RESTORED config says rather than the stale
   * pre-restore one. Same shape as health(): StorageRegistryService stays
   * unexported from the module, this facade is the only way in.
   */
  async reloadConfig(): Promise<void> {
    await this.registry.reload();
  }

  private resolve(category: ServedCategory, name: string): Resolved {
    const { driver, keyPrefix } = this.registry.resolve(category);
    const key = keyPrefix + name;
    assertValidKey(key);
    return { driver, keyPrefix, key };
  }

  /**
   * The shared local-fast-path gate for sendToResponse/withLocalFile/
   * getLocalPathOrNull: a driver's `getLocalPath` alone is never trusted as
   * proof the object is at that path — composite drivers (MirrorDriver)
   * resolve it against the primary ONLY, and even a plain local driver's
   * path can vanish between listing and this call. Null here means "no
   * usable local path" for any reason; callers fall through to the stream
   * branch, which is what actually consults a MirrorDriver's replicas.
   */
  private localPathIfPresent(driver: StorageDriver, key: string): string | null {
    const localPath = driver.getLocalPath?.(key) ?? null;
    if (localPath === null) return null;
    return fs.existsSync(localPath) ? localPath : null;
  }

  private applyHeaders(res: Response, opts?: SendOptions): void {
    if (opts?.contentType) res.setHeader('Content-Type', opts.contentType);
    if (opts?.disposition) res.setHeader('Content-Disposition', opts.disposition);
  }

  /**
   * Everything the 200/206 answer needs, staged in one place so the HEAD
   * branch and the streaming branch cannot drift apart.
   *
   * Content-Type precedence is three-way and load-bearing: an explicit
   * `opts.contentType` wins; else a type the CALLER already staged on the
   * response survives — trek-photo-cache.service.ts sets the real type before
   * calling because its objects are extension-less `.bin` blobs, and the local
   * res.sendFile branch has always preserved a pre-set type; else derive from
   * the key. Only the last case is new behaviour, and it exists because there
   * is no content sniffing here — no type at all makes a browser download an
   * image instead of showing it.
   */
  private stageServeHeaders(
    res: Response,
    key: string,
    opts: SendOptions | undefined,
    validators: Validators,
    size: number,
    range?: { start: number; end: number },
  ): void {
    // Truthiness (not `??`) mirrors applyHeaders: a blank contentType is
    // "unset", never an empty Content-Type header.
    if (opts?.contentType) {
      res.setHeader('Content-Type', opts.contentType);
    } else if (!res.getHeader('Content-Type')) {
      res.setHeader('Content-Type', contentTypeFor(key));
    }
    if (opts?.disposition) res.setHeader('Content-Disposition', opts.disposition);
    res.setHeader('Accept-Ranges', 'bytes');
    this.applyValidators(res, validators);

    if (range) {
      res.statusCode = 206;
      res.setHeader('Content-Range', `bytes ${range.start}-${range.end}/${size}`);
      res.setHeader('Content-Length', String(range.end - range.start + 1));
    } else {
      res.setHeader('Content-Length', String(size));
    }
  }

  /** Either half may be absent — see validatorsFor's degraded (mtimeMs 0) case. */
  private applyValidators(res: Response, validators: Validators): void {
    if (validators.etag) res.setHeader('ETag', validators.etag);
    if (validators.lastModified) res.setHeader('Last-Modified', validators.lastModified);
  }
}