regression: save different files with the same name and extension (#5171)
This commit is contained in:
parent
529891fcc0
commit
fba1419f90
|
@ -20,8 +20,15 @@ const sanitizeString = (value: string) => {
|
||||||
const urlWithoutQueryString = value.split('?')[0];
|
const urlWithoutQueryString = value.split('?')[0];
|
||||||
return sanitizeLikeString(urlWithoutQueryString.substring(urlWithoutQueryString.lastIndexOf('/') + 1));
|
return sanitizeLikeString(urlWithoutQueryString.substring(urlWithoutQueryString.lastIndexOf('/') + 1));
|
||||||
};
|
};
|
||||||
|
|
||||||
const serverUrlParsedAsPath = (serverURL: string) => `${sanitizeString(serverURL)}/`;
|
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 = ({
|
export const getFilename = ({
|
||||||
title,
|
title,
|
||||||
url,
|
url,
|
||||||
|
@ -97,17 +104,20 @@ const getFilePath = ({ type, mimeType, urlToCache }: { type: MediaTypes; mimeTyp
|
||||||
if (!urlToCache) {
|
if (!urlToCache) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const folderPath = getFolderPath();
|
const folderPath = getFolderPath(urlToCache);
|
||||||
const fileUrlSanitized = sanitizeString(urlToCache);
|
const urlWithoutQueryString = urlToCache.split('?')[0];
|
||||||
const filename = `${fileUrlSanitized}.${getExtension(type, mimeType)}`;
|
const filename = sanitizeFileName(getFilename({ type, mimeType, url: urlWithoutQueryString }));
|
||||||
const filePath = `${folderPath}${filename}`;
|
const filePath = `${folderPath}${filename}`;
|
||||||
return filePath;
|
return filePath;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFolderPath = () => {
|
const getFolderPath = (fileUrl: string) => {
|
||||||
const serverUrl = store.getState().server.server;
|
const serverUrl = store.getState().server.server;
|
||||||
const serverUrlParsed = serverUrlParsedAsPath(serverUrl);
|
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;
|
return folderPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -129,7 +139,7 @@ export const getMediaCache = async ({
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const folderPath = getFolderPath();
|
const folderPath = getFolderPath(urlToCache);
|
||||||
const filePath = getFilePath({ type, mimeType, urlToCache });
|
const filePath = getFilePath({ type, mimeType, urlToCache });
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -183,13 +193,14 @@ export function downloadMediaFile({
|
||||||
downloadUrl: string;
|
downloadUrl: string;
|
||||||
}): Promise<string> {
|
}): Promise<string> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
let downloadKey = '';
|
||||||
try {
|
try {
|
||||||
const path = getFilePath({ type, mimeType, urlToCache: downloadUrl });
|
const path = getFilePath({ type, mimeType, urlToCache: downloadUrl });
|
||||||
if (!path) {
|
if (!path) {
|
||||||
reject();
|
reject();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const downloadKey = mediaDownloadKey(downloadUrl);
|
downloadKey = mediaDownloadKey(downloadUrl);
|
||||||
downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path);
|
downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path);
|
||||||
const result = await downloadQueue[downloadKey].downloadAsync();
|
const result = await downloadQueue[downloadKey].downloadAsync();
|
||||||
if (result?.uri) {
|
if (result?.uri) {
|
||||||
|
@ -198,6 +209,8 @@ export function downloadMediaFile({
|
||||||
reject();
|
reject();
|
||||||
} catch {
|
} catch {
|
||||||
reject();
|
reject();
|
||||||
|
} finally {
|
||||||
|
delete downloadQueue[downloadKey];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue