Skip to content

Commit

Permalink
Fix some issues with assets with root dir path
Browse files Browse the repository at this point in the history
Close #289
  • Loading branch information
kyoshino committed Jan 4, 2025
1 parent 762706f commit f2f39dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
19 changes: 8 additions & 11 deletions src/lib/services/assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,14 @@ export const getAssetByPath = (savedPath, { entry, collection } = {}) => {
return exactMatch;
}

const { dirname: publicPath, basename: fileName } = getPathInfo(savedPath);

if (!publicPath) {
return undefined;
}
const { dirname: publicPath = '', basename: fileName } = getPathInfo(savedPath);

// eslint-disable-next-line prefer-const
let { internalPath, collectionName } =
get(allAssetFolders).findLast((folder) =>
publicPath.match(`^${folder.publicPath.replace(/{{.+?}}/g, '.+?')}\\b`),
) ?? {};
let { internalPath, collectionName } = !publicPath
? get(allAssetFolders)[0] // Global asset folder
: (get(allAssetFolders).findLast((folder) =>
publicPath.match(`^${folder.publicPath.replace(/{{.+?}}/g, '.+?')}\\b`),
) ?? {});

if (internalPath === undefined) {
return undefined;
Expand Down Expand Up @@ -434,7 +431,7 @@ export const getAssetPublicURL = (
return undefined;
}

const path = asset.path.replace(asset.folder, publicPath ?? '');
const path = asset.path.replace(asset.folder, publicPath === '/' ? '' : (publicPath ?? ''));

// Path starting with `@`, etc. cannot be linked
if (!path.startsWith('/') && !allowSpecial) {
Expand All @@ -445,7 +442,7 @@ export const getAssetPublicURL = (
return path;
}

const baseURL = get(siteConfig)?.site_url ?? '';
const baseURL = stripSlashes(get(siteConfig)?.site_url ?? '');

return `${baseURL}${path}`;
};
Expand Down
7 changes: 5 additions & 2 deletions src/lib/services/contents/draft/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,6 @@ const replaceBlobURL = async ({

const sha = await getHash(file);
const dupFile = savingAssets.find((f) => f.sha === sha);
const useSubFolder = !!publicAssetFolder && publicAssetFolder !== '/';
let assetName = '';

// Check if the file has already been added for other field or locale
Expand Down Expand Up @@ -676,7 +675,11 @@ const replaceBlobURL = async ({
blobURL,
(_match, /** @type {number} */ offset) => {
if (offset === index) {
return useSubFolder ? `${publicAssetFolder}/${assetName}` : assetName;
if (publicAssetFolder) {
return `${publicAssetFolder === '/' ? '' : publicAssetFolder}/${assetName}`;
}

return assetName;
}

return _match;
Expand Down

0 comments on commit f2f39dd

Please sign in to comment.