Skip to content

Commit

Permalink
regression: save different files with the same name and extension (#5171
Browse files Browse the repository at this point in the history
)
  • Loading branch information
reinaldonetof authored Aug 18, 2023
1 parent 529891f commit fba1419
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions app/lib/methods/handleMediaDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ const sanitizeString = (value: string) => {
const urlWithoutQueryString = value.split('?')[0];
return sanitizeLikeString(urlWithoutQueryString.substring(urlWithoutQueryString.lastIndexOf('/') + 1));
};

const serverUrlParsedAsPath = (serverURL: string) => `${sanitizeString(serverURL)}/`;

const sanitizeFileName = (value: string) => {
const extension = value.substring(value.lastIndexOf('.') + 1);
const toSanitize = value.substring(0, value.lastIndexOf('.'));
return `${sanitizeString(toSanitize)}.${extension}`;
};

export const getFilename = ({
title,
url,
Expand Down Expand Up @@ -97,17 +104,20 @@ const getFilePath = ({ type, mimeType, urlToCache }: { type: MediaTypes; mimeTyp
if (!urlToCache) {
return;
}
const folderPath = getFolderPath();
const fileUrlSanitized = sanitizeString(urlToCache);
const filename = `${fileUrlSanitized}.${getExtension(type, mimeType)}`;
const folderPath = getFolderPath(urlToCache);
const urlWithoutQueryString = urlToCache.split('?')[0];
const filename = sanitizeFileName(getFilename({ type, mimeType, url: urlWithoutQueryString }));
const filePath = `${folderPath}${filename}`;
return filePath;
};

const getFolderPath = () => {
const getFolderPath = (fileUrl: string) => {
const serverUrl = store.getState().server.server;
const serverUrlParsed = serverUrlParsedAsPath(serverUrl);
const folderPath = `${LOCAL_DOCUMENT_DIRECTORY}${serverUrlParsed}`;
const fileUrlWithoutQueryString = fileUrl.split('?')[0];
const fileUrlSplitted = fileUrlWithoutQueryString.split('/');
const messageId = fileUrlSplitted[fileUrlSplitted.length - 2];
const folderPath = `${LOCAL_DOCUMENT_DIRECTORY}${serverUrlParsed}${messageId}/`;
return folderPath;
};

Expand All @@ -129,7 +139,7 @@ export const getMediaCache = async ({
return null;
}
try {
const folderPath = getFolderPath();
const folderPath = getFolderPath(urlToCache);
const filePath = getFilePath({ type, mimeType, urlToCache });
if (!filePath) {
return null;
Expand Down Expand Up @@ -183,13 +193,14 @@ export function downloadMediaFile({
downloadUrl: string;
}): Promise<string> {
return new Promise(async (resolve, reject) => {
let downloadKey = '';
try {
const path = getFilePath({ type, mimeType, urlToCache: downloadUrl });
if (!path) {
reject();
return;
}
const downloadKey = mediaDownloadKey(downloadUrl);
downloadKey = mediaDownloadKey(downloadUrl);
downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path);
const result = await downloadQueue[downloadKey].downloadAsync();
if (result?.uri) {
Expand All @@ -198,6 +209,8 @@ export function downloadMediaFile({
reject();
} catch {
reject();
} finally {
delete downloadQueue[downloadKey];
}
});
}

0 comments on commit fba1419

Please sign in to comment.