mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-30 21:45:01 +02:00
UBERF-5595: set up attachments sizes (#4746)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import { type Attachment } from '@hcengineering/attachment'
|
||||
import { type Attachment, type AttachmentMetadata } from '@hcengineering/attachment'
|
||||
import {
|
||||
type Class,
|
||||
concatLink,
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
type Space,
|
||||
type TxOperations as Client
|
||||
} from '@hcengineering/core'
|
||||
import presentation from '@hcengineering/presentation'
|
||||
import presentation, { getFileUrl, getImageSize } from '@hcengineering/presentation'
|
||||
import { PlatformError, Severity, Status, getMetadata, setPlatformStatus, unknownError } from '@hcengineering/platform'
|
||||
|
||||
import attachment from './plugin'
|
||||
@@ -87,13 +87,16 @@ export async function createAttachments (
|
||||
const file = list.item(index)
|
||||
if (file !== null) {
|
||||
const uuid = await uploadFile(file)
|
||||
const metadata = await getAttachmentMetadata(file, uuid)
|
||||
|
||||
await client.addCollection(attachmentClass, space, objectId, objectClass, 'attachments', {
|
||||
...extraData,
|
||||
name: file.name,
|
||||
file: uuid,
|
||||
type: file.type,
|
||||
size: file.size,
|
||||
lastModified: file.lastModified
|
||||
lastModified: file.lastModified,
|
||||
metadata
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -118,3 +121,50 @@ export function getType (type: string): 'image' | 'video' | 'audio' | 'pdf' | 'o
|
||||
|
||||
return 'other'
|
||||
}
|
||||
|
||||
export async function getAttachmentMetadata (file: File, uuid: string): Promise<AttachmentMetadata | undefined> {
|
||||
const type = getType(file.type)
|
||||
|
||||
if (type === 'video') {
|
||||
const size = await getVideoSize(uuid)
|
||||
|
||||
if (size === undefined) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return {
|
||||
originalHeight: size.height,
|
||||
originalWidth: size.width
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'image') {
|
||||
const size = await getImageSize(file, getFileUrl(uuid, 'full'))
|
||||
|
||||
return {
|
||||
originalHeight: size.height,
|
||||
originalWidth: size.width,
|
||||
pixelRatio: size.pixelRatio
|
||||
}
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
async function getVideoSize (uuid: string): Promise<{ width: number, height: number } | undefined> {
|
||||
const promise = new Promise<{ width: number, height: number }>((resolve, reject) => {
|
||||
const element = document.createElement('video')
|
||||
|
||||
element.onloadedmetadata = () => {
|
||||
const height = element.videoHeight
|
||||
const width = element.videoWidth
|
||||
|
||||
resolve({ height, width })
|
||||
}
|
||||
|
||||
element.onerror = reject
|
||||
element.src = getFileUrl(uuid, 'full')
|
||||
})
|
||||
|
||||
return await promise
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user