Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions packages/keystatic/src/api/api-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export function localModeApiHandler(
config: Config,
localBaseDirectory: string | undefined
) {
const baseDirectory = path.resolve(localBaseDirectory ?? process.cwd());
// Use the base directory, and if pathPrefix is defined, resolve it relative to the base
const baseDir = localBaseDirectory ?? process.cwd();
const baseDirectory = config.storage.kind === 'local' && config.storage.pathPrefix
? path.resolve(baseDir, config.storage.pathPrefix)
: path.resolve(baseDir);
return async (
req: KeystaticRequest,
params: string[]
Expand Down Expand Up @@ -128,6 +132,10 @@ async function tree(

function getIsPathValid(config: Config) {
const allowedDirectories = getAllowedDirectories(config);
// Bypass strict path validation for local storage when pathPrefix is specified
if (config.storage.kind === 'local' && (config.storage.pathPrefix)) {
return (filepath: string) => !filepath.includes('\\');
}
return (filepath: string) =>
!filepath.includes('\\') &&
filepath.split('/').every(x => x !== '.' && x !== '..') &&
Expand Down Expand Up @@ -203,17 +211,30 @@ async function update(
} catch {
return { status: 400, body: 'Bad data' };
}
// Handle file paths when pathPrefix is configured
const pathPrefix = config.storage.kind === 'local' ? config.storage.pathPrefix : undefined;
for (const addition of updates.additions) {
await fs.mkdir(path.dirname(path.join(baseDirectory, addition.path)), {
// Construct file path based on whether we have a pathPrefix
// If we have a pathPrefix, we need to strip it from the path to avoid duplication
const effectivePath = pathPrefix && addition.path.startsWith(pathPrefix)
? addition.path.slice(pathPrefix.length + 1) // +1 for the slash
: addition.path;
const filePath = path.join(baseDirectory, effectivePath);
await fs.mkdir(path.dirname(filePath), {
recursive: true,
});
await fs.writeFile(
path.join(baseDirectory, addition.path),
filePath,
addition.contents
);
}
for (const deletion of updates.deletions) {
await fs.rm(path.join(baseDirectory, deletion.path), { force: true });
// Apply the same path correction for deletions
const effectivePath = pathPrefix && deletion.path.startsWith(pathPrefix)
? deletion.path.slice(pathPrefix.length + 1) // +1 for the slash
: deletion.path;
const filePath = path.join(baseDirectory, effectivePath);
await fs.rm(filePath, { force: true });
}
return {
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion packages/keystatic/src/app/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export type FormatInfo = {
};

export function getPathPrefix(storage: Config['storage']) {
if (storage.kind === 'local' || !storage.pathPrefix) {
if (!storage.pathPrefix) {
return undefined;
}
return fixPath(storage.pathPrefix) + '/';
Expand Down
5 changes: 4 additions & 1 deletion packages/keystatic/src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export type GitHubConfig<
singletons?: Singletons;
} & CommonConfig<Collections, Singletons>;

type LocalStorageConfig = { kind: 'local' };
type LocalStorageConfig = {
kind: 'local';
pathPrefix?: string;
};

export type LocalConfig<
Collections extends {
Expand Down