Browse Source

refactor(nc-gui): getImageDataFromUrl function

pull/7605/head
Ramesh Mane 9 months ago
parent
commit
999bade32d
  1. 3
      packages/nc-gui/components/cell/attachment/utils.ts
  2. 8
      packages/nc-gui/utils/fileUtils.ts

3
packages/nc-gui/components/cell/attachment/utils.ts

@ -13,7 +13,6 @@ import {
extractImageSrcFromRawHtml, extractImageSrcFromRawHtml,
inject, inject,
isImage, isImage,
isImageUrl,
message, message,
parseProp, parseProp,
ref, ref,
@ -332,7 +331,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
mimetype: response.headers.get('content-type') || undefined, mimetype: response.headers.get('content-type') || undefined,
size: +(response.headers.get('content-length') || 0) || undefined, size: +(response.headers.get('content-length') || 0) || undefined,
} as { minetype?: string; size?: number } } as { minetype?: string; size?: number }
} else if (isImageUrl(imageUrl)) { } else if (imageUrl.slice(imageUrl.lastIndexOf('.') + 1).toLowerCase().length) {
return { return {
mimetype: `image/${imageUrl.slice(imageUrl.lastIndexOf('.') + 1).toLowerCase()}`, mimetype: `image/${imageUrl.slice(imageUrl.lastIndexOf('.') + 1).toLowerCase()}`,
size: +(response.headers.get('content-length') || 0) || undefined, size: +(response.headers.get('content-length') || 0) || undefined,

8
packages/nc-gui/utils/fileUtils.ts

@ -19,11 +19,7 @@ const isImage = (name: string, mimetype?: string) => {
return imageExt.some((e) => name?.toLowerCase().endsWith(`.${e}`)) || mimetype?.startsWith('image/') return imageExt.some((e) => name?.toLowerCase().endsWith(`.${e}`)) || mimetype?.startsWith('image/')
} }
const isImageUrl = (url: string) => { export { isImage, imageExt }
return imageExt.some((e) => url?.toLowerCase().endsWith(`.${e}`))
}
export { isImage, imageExt, isImageUrl }
// Ref : https://stackoverflow.com/a/12002275 // Ref : https://stackoverflow.com/a/12002275
// Tested in Mozilla Firefox browser, Chrome // Tested in Mozilla Firefox browser, Chrome
@ -83,6 +79,8 @@ export function extractImageSrcFromRawHtml(rawText: string) {
} }
export function populateUniqueFileName(fn: string, attachments: any[], mimeType: string) { export function populateUniqueFileName(fn: string, attachments: any[], mimeType: string) {
if (!mimeType) return fn
// If the image extension is not present, the while loop will go into an infinite loop. So, add the extension first if not present. // If the image extension is not present, the while loop will go into an infinite loop. So, add the extension first if not present.
if (!fn?.endsWith(mimeType.split('/')[1])) { if (!fn?.endsWith(mimeType.split('/')[1])) {
fn = `${fn}.${mimeType.split('/')[1]}` fn = `${fn}.${mimeType.split('/')[1]}`

Loading…
Cancel
Save