replace invalid unicode in filename

pull/2137/head
audric 3 years ago
parent 72409e3f1f
commit 4b39b46b6a

@ -381,7 +381,6 @@ export async function uploadOurAvatar(newAvatarDecrypted?: ArrayBuffer) {
isRaw: true,
data: decryptedAvatarData,
contentType: MIME.IMAGE_UNKNOWN, // contentType is mostly used to generate previews and screenshot. We do not care for those in this case.
// url: fileUrl,
});
// Replace our temporary image with the attachment pointer from the server:
ourConvo.set('avatar', null);

@ -152,7 +152,7 @@ async function processNormalAttachments(
if (message.isTrustedForAttachmentDownload()) {
const openGroupV2Details = (isOpenGroupV2 && convo.toOpenGroupV2()) || undefined;
const attachments = await Promise.all(
normalAttachments.map(async (attachment: any, index: any) => {
normalAttachments.map(async (attachment: any, index: number) => {
return AttachmentDownloads.addJob(attachment, {
messageId: message.id,
type: 'attachment',
@ -177,7 +177,7 @@ async function processPreviews(message: MessageModel, convo: ConversationModel):
const openGroupV2Details = (isOpenGroupV2 && convo.toOpenGroupV2()) || undefined;
const preview = await Promise.all(
(message.get('preview') || []).map(async (item: any, index: any) => {
(message.get('preview') || []).map(async (item: any, index: number) => {
if (!item.image) {
return item;
}

@ -100,7 +100,7 @@ export const getDecryptedMediaUrl = async (
forceRetain: isAvatar,
});
}
window.log.info(' file decrypted :', url);
window.log.info(' file decrypted :', url, ' as ', obj);
urlToDecryptingPromise.delete(url);
resolve(obj);
return;

@ -208,6 +208,7 @@ async function _runJob(job: any) {
}
const upgradedAttachment = await processNewAttachment({
...downloaded,
fileName: attachment.fileName,
contentType: attachment.contentType,
});
found = await getMessageById(messageId);

@ -12,6 +12,7 @@ import {
captureDimensionsAndScreenshot,
deleteData,
loadData,
replaceUnicodeV2,
} from './attachments/migrations';
// tslint:disable: prefer-object-spread
@ -160,31 +161,28 @@ export const loadQuoteData = async (quote: any) => {
};
export const processNewAttachment = async (attachment: {
fileName?: string;
contentType: string;
data: ArrayBuffer;
digest?: string;
path?: string;
isRaw?: boolean;
}) => {
const fileName = attachment.fileName ? replaceUnicodeV2(attachment.fileName) : '';
// this operation might change the size (as we might print the content to a canvas and get the data back)
const rotatedData = await autoOrientJPEGAttachment(attachment);
const rotatedAttachment = {
...attachment,
contentType: rotatedData.contentType,
data: rotatedData.data,
digest: attachment.digest as string | undefined,
};
const onDiskAttachmentPath = await migrateDataToFileSystem(rotatedData.data);
const attachmentWithoutData = omit({ ...attachment, fileName, path: onDiskAttachmentPath }, [
'data',
]);
if (rotatedData.shouldDeleteDigest) {
delete rotatedAttachment.digest;
delete attachmentWithoutData.digest;
}
const onDiskAttachmentPath = await migrateDataToFileSystem(rotatedAttachment.data);
const attachmentWithoutData = omit({ ...attachment, path: onDiskAttachmentPath }, ['data']);
const finalAttachment = await captureDimensionsAndScreenshot(attachmentWithoutData);
return { ...finalAttachment, size: rotatedAttachment.data.byteLength };
return { ...finalAttachment, fileName, size: rotatedData.data.byteLength };
};
export const readAttachmentData = async (relativePath: string): Promise<ArrayBufferLike> => {

@ -112,25 +112,20 @@ export const _replaceUnicodeOrderOverridesSync = (attachment: any) => {
// const replaceUnicodeOrderOverrides = async (attachment: any) =>
// _replaceUnicodeOrderOverridesSync(attachment);
// // \u202A-\u202E is LRE, RLE, PDF, LRO, RLO
// // \u2066-\u2069 is LRI, RLI, FSI, PDI
// // \u200E is LRM
// // \u200F is RLM
// // \u061C is ALM
// const V2_UNWANTED_UNICODE = /[\u202A-\u202E\u2066-\u2069\u200E\u200F\u061C]/g;
// const replaceUnicodeV2 = async (attachment: any) => {
// if (!isString(attachment.fileName)) {
// return attachment;
// }
// const fileName = attachment.fileName.replace(V2_UNWANTED_UNICODE, UNICODE_REPLACEMENT_CHARACTER);
// \u202A-\u202E is LRE, RLE, PDF, LRO, RLO
// \u2066-\u2069 is LRI, RLI, FSI, PDI
// \u200E is LRM
// \u200F is RLM
// \u061C is ALM
const V2_UNWANTED_UNICODE = /[\u202A-\u202E\u2066-\u2069\u200E\u200F\u061C]/g;
export const replaceUnicodeV2 = (fileName: string) => {
if (!isString(fileName)) {
throw new Error('replaceUnicodeV2 should not be called without a filename');
}
// return {
// ...attachment,
// fileName,
// };
// };
return fileName.replace(V2_UNWANTED_UNICODE, UNICODE_REPLACEMENT_CHARACTER);
};
// const removeSchemaVersion = ({ attachment }: any) => {
// if (!isValid(attachment)) {
@ -196,6 +191,7 @@ export const captureDimensionsAndScreenshot = async (
): Promise<CaptureDimensionType & {
width?: number;
height?: number;
thumbnail: {
path: string;
contentType: string;

@ -40,6 +40,8 @@ export interface MaxScaleSize {
export const ATTACHMENT_DEFAULT_MAX_SIDE = 4096;
export const AVATAR_MAX_SIDE = 640;
/**
* Resize a jpg/gif/png file to our definition on an avatar before upload
*/
@ -47,7 +49,7 @@ export async function autoScaleForAvatar<T extends { contentType: string; blob:
attachment: T
) {
const maxMeasurements = {
maxSide: 640,
maxSide: AVATAR_MAX_SIDE,
maxSize: 1000 * 1024,
};
@ -70,7 +72,7 @@ export async function autoScaleForAvatar<T extends { contentType: string; blob:
*/
export async function autoScaleForIncomingAvatar(incomingAvatar: ArrayBuffer) {
const maxMeasurements = {
maxSide: 640,
maxSide: AVATAR_MAX_SIDE,
maxSize: 1000 * 1024,
};
@ -269,12 +271,13 @@ export async function getFileAndStoreLocally(
const attachmentSavedLocally = await processNewAttachment({
data: await scaled.blob.arrayBuffer(),
contentType: attachment.contentType,
fileName: attachment.fileName,
});
return {
caption: attachment.caption,
contentType: attachment.contentType,
fileName: attachment.fileName,
fileName: attachmentSavedLocally.fileName,
path: attachmentSavedLocally.path,
width: attachmentSavedLocally.width,
height: attachmentSavedLocally.height,

Loading…
Cancel
Save