2024-01-18 13:20:10 +02:00
|
|
|
import type { ResourceEntity } from '../../services/database/types';
|
2024-06-04 10:50:18 +02:00
|
|
|
import * as mime from '../../mime-utils';
|
2024-01-18 13:20:10 +02:00
|
|
|
import { filename } from '@joplin/utils/path';
|
|
|
|
|
|
|
|
// This file contains resource-related utilities that do not
|
|
|
|
// depend on the database, settings, etc.
|
|
|
|
|
|
|
|
export const resourceFilename = (resource: ResourceEntity, encryptedBlob = false) => {
|
|
|
|
let extension = encryptedBlob ? 'crypted' : resource.file_extension;
|
|
|
|
if (!extension) extension = resource.mime ? mime.toFileExtension(resource.mime) : '';
|
|
|
|
extension = extension ? `.${extension}` : '';
|
|
|
|
return resource.id + extension;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resourceRelativePath = (resource: ResourceEntity, relativeResourceDirPath: string, encryptedBlob = false) => {
|
|
|
|
return `${relativeResourceDirPath}/${resourceFilename(resource, encryptedBlob)}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resourceFullPath = (resource: ResourceEntity, resourceDirPath: string, encryptedBlob = false) => {
|
|
|
|
return `${resourceDirPath}/${resourceFilename(resource, encryptedBlob)}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const internalUrl = (resource: ResourceEntity) => {
|
|
|
|
return `:/${resource.id}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resourcePathToId = (path: string) => {
|
|
|
|
return filename(path);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isResourceUrl = (url: string) => {
|
|
|
|
return url && url.length === 34 && url[0] === ':' && url[1] === '/';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resourceUrlToId = (url: string) => {
|
|
|
|
if (!isResourceUrl(url)) throw new Error(`Not a valid resource URL: ${url}`);
|
|
|
|
return url.substring(2);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isSupportedImageMimeType = (type: string) => {
|
2024-08-22 22:53:44 +02:00
|
|
|
const imageMimeTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'image/webp', 'image/avif', 'image/bmp'];
|
2024-01-18 13:20:10 +02:00
|
|
|
return imageMimeTypes.indexOf(type.toLowerCase()) >= 0;
|
|
|
|
};
|