Changeset View
Changeset View
Standalone View
Standalone View
lib/media/media-utils.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import type { Media } from '../types/media-types.js'; | import type { Media } from '../types/media-types.js'; | ||||
import type { | import type { | ||||
MultimediaMessageInfo, | MultimediaMessageInfo, | ||||
RawMultimediaMessageInfo, | RawMultimediaMessageInfo, | ||||
} from '../types/message-types.js'; | } from '../types/message-types.js'; | ||||
import { isBlobServiceURI } from '../utils/blob-service.js'; | |||||
const maxDimensions = Object.freeze({ width: 1920, height: 1920 }); | const maxDimensions = Object.freeze({ width: 1920, height: 1920 }); | ||||
function contentStringForMediaArray(media: $ReadOnlyArray<Media>): string { | function contentStringForMediaArray(media: $ReadOnlyArray<Media>): string { | ||||
if (media.length === 0) { | if (media.length === 0) { | ||||
return 'corrupted media'; | return 'corrupted media'; | ||||
} else if (media.length === 1) { | } else if (media.length === 1) { | ||||
const type = media[0].type.replace('encrypted_', ''); | const type = media[0].type.replace('encrypted_', ''); | ||||
Show All 13 Lines | function contentStringForMediaArray(media: $ReadOnlyArray<Media>): string { | ||||
invariant(firstType, 'there should be some media'); | invariant(firstType, 'there should be some media'); | ||||
firstType = firstType.replace('encrypted_', ''); | firstType = firstType.replace('encrypted_', ''); | ||||
if (firstType === 'photo') { | if (firstType === 'photo') { | ||||
firstType = 'image'; | firstType = 'image'; | ||||
} | } | ||||
return `some ${firstType}s`; | return `some ${firstType}s`; | ||||
} | } | ||||
function isMediaBlobServiceHosted(media: Media): boolean { | |||||
return ( | |||||
(!!media.uri && isBlobServiceURI(media.uri)) || | |||||
(!!media.holder && isBlobServiceURI(media.holder)) || | |||||
(!!media.thumbnailURI && isBlobServiceURI(media.thumbnailURI)) || | |||||
(!!media.thumbnailHolder && isBlobServiceURI(media.thumbnailHolder)) | |||||
); | |||||
} | |||||
function multimediaMessagePreview( | function multimediaMessagePreview( | ||||
messageInfo: MultimediaMessageInfo | RawMultimediaMessageInfo, | messageInfo: MultimediaMessageInfo | RawMultimediaMessageInfo, | ||||
): string { | ): string { | ||||
const mediaContentString = contentStringForMediaArray(messageInfo.media); | const mediaContentString = contentStringForMediaArray(messageInfo.media); | ||||
return `sent ${mediaContentString}`; | return `sent ${mediaContentString}`; | ||||
} | } | ||||
const localUploadPrefix = 'localUpload'; | const localUploadPrefix = 'localUpload'; | ||||
function isLocalUploadID(id: string): boolean { | function isLocalUploadID(id: string): boolean { | ||||
return id.startsWith(localUploadPrefix); | return id.startsWith(localUploadPrefix); | ||||
} | } | ||||
let nextLocalUploadID = 0; | let nextLocalUploadID = 0; | ||||
function getNextLocalUploadID(): string { | function getNextLocalUploadID(): string { | ||||
return `${localUploadPrefix}${nextLocalUploadID++}`; | return `${localUploadPrefix}${nextLocalUploadID++}`; | ||||
} | } | ||||
export { | export { | ||||
maxDimensions, | maxDimensions, | ||||
contentStringForMediaArray, | contentStringForMediaArray, | ||||
multimediaMessagePreview, | multimediaMessagePreview, | ||||
isLocalUploadID, | isLocalUploadID, | ||||
isMediaBlobServiceHosted, | |||||
getNextLocalUploadID, | getNextLocalUploadID, | ||||
}; | }; |