import type { DriveFileInfo } from "@/lib/api/types" import { drivePreviewKind, isPreviewNavigable, shouldOpenInOnlyOffice, toPreviewTarget, } from "@/lib/drive/drive-preview" import { fetchPublicShareBlob, publicShareDownloadApiPath, publicShareHref } from "@/lib/api/public-share" import { buildPublicShareEditHref } from "@/lib/drive/public-share-url" import type { DrivePreviewContext } from "@/lib/stores/drive-ui-store" export interface OpenPublicShareItemRouter { push: (href: string) => void } export interface OpenPublicShareItemOptions { token: string password?: string canEdit: boolean router: OpenPublicShareItemRouter openPreview: ( files: ReturnType[], index: number, context?: Partial ) => void contextItems?: DriveFileInfo[] } export function openPublicShareItem(file: DriveFileInfo, options: OpenPublicShareItemOptions) { const { token, password, canEdit, router, openPreview, contextItems = [file] } = options if (file.type === "directory") { router.push(publicShareHref(token, file.path)) return } if (drivePreviewKind(file)) { const navigable = contextItems .filter((item) => isPreviewNavigable(item)) .map((item) => toPreviewTarget(item)) const index = navigable.findIndex((item) => item.path === file.path) openPreview(navigable, index >= 0 ? index : 0, { allowShare: false, isTrash: false, publicShare: { token, password, canEdit }, }) return } if (shouldOpenInOnlyOffice(file)) { const returnTo = typeof window !== "undefined" ? window.location.pathname + window.location.search : undefined const mode = canEdit ? "edit" : "view" router.push(buildPublicShareEditHref(token, file.path, returnTo, mode)) return } const url = publicShareDownloadApiPath(token, file.path, password) const anchor = document.createElement("a") anchor.href = url anchor.download = file.name anchor.rel = "noopener" document.body.appendChild(anchor) anchor.click() anchor.remove() } export async function downloadPublicShareFile( token: string, file: DriveFileInfo, password?: string ) { const blob = await fetchPublicShareBlob(token, file, password) const objectUrl = URL.createObjectURL(blob) const anchor = document.createElement("a") anchor.href = objectUrl anchor.download = file.name document.body.appendChild(anchor) anchor.click() anchor.remove() URL.revokeObjectURL(objectUrl) }