Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

4.0 KB
Raw Blame
import { Attachment, MotionMediaFamily, MotionMediaRole } from "@/types/proto/api/attachment_service_pb";

export const getAttachmentUrl = (attachment: Attachment) => {
  if (attachment.externalLink) {
    return attachment.externalLink;
  }

  return `${window.location.origin}/file/${attachment.name}/${encodeURIComponent(attachment.filename)}`;
};

// Appends a thumbnail or motion selector only to share-mode links; other external links fall back to the server-managed attachment URL.
const withShareTokenParam = (externalLink: string | undefined, key: string): string | undefined => {
  if (!externalLink) {
    return undefined;
  }
  const url = new URL(externalLink, window.location.origin);
  if (!url.searchParams.has("share_token")) {
    return undefined;
  }
  url.searchParams.set(key, "true");
  return url.toString();
};

export const getAttachmentThumbnailUrl = (attachment: Attachment) => {
  const shareUrl = withShareTokenParam(attachment.externalLink, "thumbnail");
  if (shareUrl) {
    return shareUrl;
  }

  return `${window.location.origin}/file/${attachment.name}/${encodeURIComponent(attachment.filename)}?thumbnail=true`;
};

export const getAttachmentMotionClipUrl = (attachment: Attachment) => {
  const shareUrl = withShareTokenParam(attachment.externalLink, "motion");
  if (shareUrl) {
    return shareUrl;
  }

  return `${window.location.origin}/file/${attachment.name}/${encodeURIComponent(attachment.filename)}?motion=true`;
};

export const getAttachmentType = (attachment: Attachment) => {
  if (isImage(attachment.type)) {
    return "image/*";
  } else if (attachment.type.startsWith("video")) {
    return "video/*";
  } else if (attachment.type.startsWith("audio") && !isMidiFile(attachment.type)) {
    return "audio/*";
  } else if (attachment.type.startsWith("text")) {
    return "text/*";
  } else if (attachment.type.startsWith("application/epub+zip")) {
    return "application/epub+zip";
  } else if (attachment.type.startsWith("application/pdf")) {
    return "application/pdf";
  } else if (attachment.type.includes("word")) {
    return "application/msword";
  } else if (attachment.type.includes("excel")) {
    return "application/msexcel";
  } else if (attachment.type.startsWith("application/zip")) {
    return "application/zip";
  } else if (attachment.type.startsWith("application/x-java-archive")) {
    return "application/x-java-archive";
  } else {
    return "application/octet-stream";
  }
};

// isImage returns true if the given mime type is an image.
export const isImage = (t: string) => {
  // Don't show PSDs as images.
  return t.startsWith("image/") && !isPSD(t);
};

// isMidiFile returns true if the given mime type is a MIDI file.
export const isMidiFile = (mimeType: string): boolean => {
  return mimeType === "audio/midi" || mimeType === "audio/mid" || mimeType === "audio/x-midi" || mimeType === "application/x-midi";
};

const isPSD = (t: string) => {
  return t === "image/vnd.adobe.photoshop" || t === "image/x-photoshop" || t === "image/photoshop";
};

export const getAttachmentMotionGroupId = (attachment: Attachment): string | undefined => {
  return attachment.motionMedia?.groupId || undefined;
};

export const isAppleLivePhotoStill = (attachment: Attachment): boolean =>
  attachment.motionMedia?.family === MotionMediaFamily.APPLE_LIVE_PHOTO && attachment.motionMedia.role === MotionMediaRole.STILL;

export const isAppleLivePhotoVideo = (attachment: Attachment): boolean =>
  attachment.motionMedia?.family === MotionMediaFamily.APPLE_LIVE_PHOTO && attachment.motionMedia.role === MotionMediaRole.VIDEO;

export const isAndroidMotionContainer = (attachment: Attachment): boolean =>
  attachment.motionMedia?.family === MotionMediaFamily.ANDROID_MOTION_PHOTO &&
  attachment.motionMedia.role === MotionMediaRole.CONTAINER &&
  attachment.motionMedia.hasEmbeddedVideo;

export const isMotionAttachment = (attachment: Attachment): boolean =>
  isAppleLivePhotoStill(attachment) || isAppleLivePhotoVideo(attachment) || isAndroidMotionContainer(attachment);