Files
nocodb/packages/nc-gui/utils/attachmentUtils.ts
Anbarasu 64a66b48a0 feat: attachments by url, webcam (#8921)
* fix: attachment fixes

* feat: support camera, local rewrite uploads feat: url, wip

* fix: upload url

* fix: import useAttachmentCell

* fix: handle invalid urls, edge cases

* fix: handle stopping all active streams

* fix: styling

* fix: styling

* fix: tests correction

* fix: bulk updates tests

* fix: thumbnail support, fix: icons for all fix: url with redirect not working fix: overflow issue fix: scope style modals fix: expanded form design broken

* fix: clear all active streams

* fix: mirror camera

* fix: update colors fix: enable upload-via-url in shared views

* fix: upload-via url

* fix: typo

* fix: update styles fix: added localization fix: issues with camera

* fix: update translation

* fix: show error inline fix: icon hover effect not working fix: reuse media stream

* fix: slowui

* fix: don't load if the filetype does not starts with image/video

* fix: translation

---------

Co-authored-by: Raju Udava <86527202+dstala@users.noreply.github.com>
2024-07-05 16:43:51 +05:30

71 lines
2.3 KiB
TypeScript

export const createThumbnail = async (file: File): Promise<string | null> => {
if (!file.type.startsWith('image/') && !file.type.startsWith('video/')) return null
return new Promise<string | null>((resolve, reject) => {
const reader = new FileReader()
reader.onload = function (event) {
const thumbnailURL = event.target?.result as string
if (file.type.startsWith('image/')) {
const img = new Image()
img.onload = function () {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
if (!ctx) return resolve(null)
const thumbnailWidth = 200
const scaleFactor = thumbnailWidth / img.width
const thumbnailHeight = img.height * scaleFactor
canvas.width = thumbnailWidth
canvas.height = thumbnailHeight
ctx.drawImage(img, 0, 0, thumbnailWidth, thumbnailHeight)
const thumbnailDataURL = canvas.toDataURL('image/png')
resolve(thumbnailDataURL)
}
img.onerror = function () {
console.error('Error loading image')
reject(null)
}
img.src = thumbnailURL
} else if (file.type.startsWith('video/')) {
const video = document.createElement('video')
video.onloadedmetadata = function () {
video.currentTime = 1
}
video.onseeked = function () {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
if (!ctx) return resolve(null)
const thumbnailWidth = 200
const scaleFactor = thumbnailWidth / video.videoWidth
const thumbnailHeight = video.videoHeight * scaleFactor
canvas.width = thumbnailWidth
canvas.height = thumbnailHeight
ctx.drawImage(video, 0, 0, thumbnailWidth, thumbnailHeight)
const thumbnailDataURL = canvas.toDataURL('image/png')
resolve(thumbnailDataURL)
}
video.onerror = function () {
console.error('Error loading video')
reject(null)
}
video.src = thumbnailURL
} else {
resolve(null)
}
}
reader.onerror = function () {
console.error('Error reading file')
reject(null)
}
reader.readAsDataURL(file)
})
}