Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1013 B
Raw Blame
import type { FileId, StirlingFileStub } from "@editor/types/fileContext";
import {
  downloadFromUrl,
  DownloadResult,
} from "@editor/services/downloadService";

export interface OperationSaveContext {
  downloadUrl: string | null;
  downloadFilename: string;
  downloadLocalPath?: string | null;
  outputFileIds?: string[] | null;
  getFile: (fileId: FileId) => File | undefined;
  getStub: (fileId: FileId) => StirlingFileStub | undefined;
  markSaved: (fileId: FileId, savedPath?: string) => void;
}

export async function saveOperationResults(
  context: OperationSaveContext,
): Promise<DownloadResult | null> {
  if (!context.downloadUrl) return null;

  const result = await downloadFromUrl(
    context.downloadUrl,
    context.downloadFilename || "download",
    context.downloadLocalPath || undefined,
  );

  if (context.outputFileIds && result.savedPath) {
    for (const fileId of context.outputFileIds) {
      context.markSaved(fileId as FileId, result.savedPath);
    }
  }

  return result;
}