Commit 53fe3631 by Ryo Committed by GitHub

fix: api dataset filename fallback (#6527)

parent 8eb841cc
...@@ -92,6 +92,26 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer } ...@@ -92,6 +92,26 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer }
.catch((err) => responseError(err)); .catch((err) => responseError(err));
}; };
const getPreviewUrlFilename = (previewUrl: string) => {
const parseFilename = (pathname: string) => {
const filename = pathname.split('/').pop() || '';
if (!filename) return '';
try {
return decodeURIComponent(filename);
} catch {
return filename;
}
};
try {
return parseFilename(new URL(previewUrl).pathname);
} catch {
return parseFilename(previewUrl.split('?')[0].split('#')[0]);
}
};
const listFiles = async ({ const listFiles = async ({
searchKey, searchKey,
parentId parentId
...@@ -163,7 +183,7 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer } ...@@ -163,7 +183,7 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer }
}); });
if (rawTextBuffer) { if (rawTextBuffer) {
return { return {
title, title: title || rawTextBuffer.filename || getPreviewUrlFilename(previewUrl),
rawText: rawTextBuffer.text rawText: rawTextBuffer.text
}; };
} }
...@@ -178,7 +198,7 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer } ...@@ -178,7 +198,7 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer }
getFormatText: true getFormatText: true
}); });
const sourceName = title || getNanoid(); const sourceName = title || getPreviewUrlFilename(previewUrl) || getNanoid();
getS3RawTextSource().addRawTextBuffer({ getS3RawTextSource().addRawTextBuffer({
sourceId: previewUrl, sourceId: previewUrl,
...@@ -188,7 +208,7 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer } ...@@ -188,7 +208,7 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer }
}); });
return { return {
title, title: title || sourceName,
rawText rawText
}; };
} }
......
...@@ -202,7 +202,7 @@ export const syncCollection = async (collection: CollectionWithDatasetType) => { ...@@ -202,7 +202,7 @@ export const syncCollection = async (collection: CollectionWithDatasetType) => {
}); });
return DatasetCollectionSyncResultEnum.success; return DatasetCollectionSyncResultEnum.success;
} else if (collection.name !== title) { } else if (title && collection.name !== title) {
await MongoDatasetCollection.updateOne({ _id: collection._id }, { $set: { name: title } }); await MongoDatasetCollection.updateOne({ _id: collection._id }, { $set: { name: title } });
return DatasetCollectionSyncResultEnum.success; return DatasetCollectionSyncResultEnum.success;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment