Reuse media content/info types from the js-sdk (#12308)

This commit is contained in:
Michael Telatynski 2024-03-11 09:30:00 +00:00 committed by GitHub
parent 89eb884821
commit 431ae32304
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 74 additions and 258 deletions

View file

@ -17,9 +17,9 @@ limitations under the License.
// Pull in the encryption lib so that we can decrypt attachments.
import encrypt from "matrix-encrypt-attachment";
import { parseErrorResponse } from "matrix-js-sdk/src/matrix";
import { EncryptedFile, MediaEventInfo } from "matrix-js-sdk/src/types";
import { mediaFromContent } from "../customisations/Media";
import { EncryptedFile, IMediaEventInfo } from "../customisations/models/IMediaEventContent";
import { getBlobSafeMimeType } from "./blobs";
export class DownloadError extends Error {
@ -44,10 +44,10 @@ export class DecryptError extends Error {
* This passed to [link]{@link https://github.com/matrix-org/matrix-encrypt-attachment}
* as the encryption info object, so will also have the those keys in addition to
* the keys below.
* @param {IMediaEventInfo} info The info parameter taken from the matrix event.
* @param {MediaEventInfo} info The info parameter taken from the matrix event.
* @returns {Promise<Blob>} Resolves to a Blob of the file.
*/
export async function decryptFile(file?: EncryptedFile, info?: IMediaEventInfo): Promise<Blob> {
export async function decryptFile(file?: EncryptedFile, info?: MediaEventInfo): Promise<Blob> {
// throws if file is falsy
const media = mediaFromContent({ file });

View file

@ -25,22 +25,22 @@ import {
FileSizeReturnArray,
FileSizeReturnObject,
} from "filesize";
import { MediaEventContent } from "matrix-js-sdk/src/types";
import { IMediaEventContent } from "../customisations/models/IMediaEventContent";
import { _t } from "../languageHandler";
/**
* Extracts a human-readable label for the file attachment to use as
* link text.
*
* @param {IMediaEventContent} content The "content" key of the matrix event.
* @param {MediaEventContent} content The "content" key of the matrix event.
* @param {string} fallbackText The fallback text
* @param {boolean} withSize Whether to include size information. Default true.
* @param {boolean} shortened Ensure the extension of the file name is visible. Default false.
* @return {string} the human-readable link text for the attachment.
*/
export function presentableTextForFile(
content: IMediaEventContent,
content: MediaEventContent,
fallbackText = _t("common|attachment"),
withSize = true,
shortened = false,

View file

@ -15,12 +15,12 @@ limitations under the License.
*/
import { MatrixEvent, EventType, MsgType } from "matrix-js-sdk/src/matrix";
import { FileContent, ImageContent, MediaEventContent } from "matrix-js-sdk/src/types";
import { logger } from "matrix-js-sdk/src/logger";
import { LazyValue } from "./LazyValue";
import { Media, mediaFromContent } from "../customisations/Media";
import { decryptFile } from "./DecryptFile";
import { FileContent, ImageContent, IMediaEventContent } from "../customisations/models/IMediaEventContent";
import { IDestroyable } from "./IDestroyable";
// TODO: We should consider caching the blobs. https://github.com/vector-im/element-web/issues/17192
@ -48,7 +48,7 @@ export class MediaEventHelper implements IDestroyable {
public get fileName(): string {
return (
this.event.getContent<FileContent>().filename ||
this.event.getContent<IMediaEventContent>().body ||
this.event.getContent<MediaEventContent>().body ||
"download"
);
}
@ -81,7 +81,7 @@ export class MediaEventHelper implements IDestroyable {
private fetchSource = (): Promise<Blob> => {
if (this.media.isEncrypted) {
const content = this.event.getContent<IMediaEventContent>();
const content = this.event.getContent<MediaEventContent>();
return decryptFile(content.file!, content.info);
}
return this.media.downloadSource().then((r) => r.blob());

View file

@ -15,6 +15,7 @@ limitations under the License.
*/
import { Direction, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { MediaEventContent } from "matrix-js-sdk/src/types";
import { saveAs } from "file-saver";
import { logger } from "matrix-js-sdk/src/logger";
import sanitizeFilename from "sanitize-filename";
@ -24,7 +25,6 @@ import { decryptFile } from "../DecryptFile";
import { mediaFromContent } from "../../customisations/Media";
import { formatFullDateNoDay, formatFullDateNoDayISO } from "../../DateUtils";
import { isVoiceMessage } from "../EventUtils";
import { IMediaEventContent } from "../../customisations/models/IMediaEventContent";
import { _t } from "../../languageHandler";
import SdkConfig from "../../SdkConfig";
@ -225,7 +225,7 @@ export default abstract class Exporter {
let blob: Blob | undefined = undefined;
try {
const isEncrypted = event.isEncrypted();
const content = event.getContent<IMediaEventContent>();
const content = event.getContent<MediaEventContent>();
const shouldDecrypt = isEncrypted && content.hasOwnProperty("file") && event.getType() !== "m.sticker";
if (shouldDecrypt) {
blob = await decryptFile(content.file);

View file

@ -14,8 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { EncryptedFile } from "matrix-js-sdk/src/types";
import { BlurhashEncoder } from "../BlurhashEncoder";
import { EncryptedFile } from "../customisations/models/IMediaEventContent";
type ThumbnailableElement = HTMLImageElement | HTMLVideoElement;