Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Feb 4, 2025
1 parent 495d72e commit 40630a6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
19 changes: 17 additions & 2 deletions packages/backend/src/server/api/endpoints/pages/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export const meta = {
code: 'NO_SUCH_FILE',
id: 'b7b97489-0f66-4b12-a5ff-b21bd63f6e1c',
},
invalidName: {
message: 'Invalid name.',
code: 'INVALID_NAME',
id: '8702f702-f18f-4657-b50b-f746a3dffd3c',
},
nameAlreadyExists: {
message: 'Specified name already exists.',
code: 'NAME_ALREADY_EXISTS',
Expand Down Expand Up @@ -81,6 +86,16 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private idService: IdService,
) {
super(meta, paramDef, async (ps, me) => {
const trimmedName = ps.name.trim();

if (trimmedName.trim() === '') {
throw new ApiError(meta.errors.invalidName);
}

if ([' ', '/', '\\', '.', '#', '&', '%', '?', '!', '+', '<', '>'].some(c => trimmedName.includes(c))) {
throw new ApiError(meta.errors.invalidName);
}

let eyeCatchingImage = null;
if (ps.eyeCatchingImageId != null) {
eyeCatchingImage = await this.driveFilesRepository.findOneBy({
Expand All @@ -95,7 +110,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-

await this.pagesRepository.findBy({
userId: me.id,
name: ps.name,
name: trimmedName,
}).then(result => {
if (result.length > 0) {
throw new ApiError(meta.errors.nameAlreadyExists);
Expand All @@ -106,7 +121,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
id: this.idService.gen(),
updatedAt: new Date(),
title: ps.title,
name: ps.name,
name: trimmedName,
summary: ps.summary,
content: ps.content,
variables: ps.variables,
Expand Down
21 changes: 17 additions & 4 deletions packages/backend/src/server/api/endpoints/pages/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ export const meta = {
code: 'NO_SUCH_PAGE',
id: '21149b9e-3616-4778-9592-c4ce89f5a864',
},

accessDenied: {
message: 'Access denied.',
code: 'ACCESS_DENIED',
id: '3c15cd52-3b4b-4274-967d-6456fc4f792b',
},

invalidName: {
message: 'Invalid name.',
code: 'INVALID_NAME',
id: '75a78404-bcd1-4d98-9354-25c60f930e78',
},
noSuchFile: {
message: 'No such file.',
code: 'NO_SUCH_FILE',
Expand Down Expand Up @@ -103,10 +106,20 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}

if (ps.name != null) {
const trimmedName = ps.name.trim();

if (trimmedName.trim() === '') {
throw new ApiError(meta.errors.invalidName);
}

if ([' ', '/', '\\', '.', '#', '&', '%', '?', '!', '+', '<', '>'].some(c => trimmedName.includes(c))) {
throw new ApiError(meta.errors.invalidName);
}

await this.pagesRepository.findBy({
id: Not(ps.pageId),
userId: me.id,
name: ps.name,
name: trimmedName,
}).then(result => {
if (result.length > 0) {
throw new ApiError(meta.errors.nameAlreadyExists);
Expand All @@ -117,7 +130,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
await this.pagesRepository.update(page.id, {
updatedAt: new Date(),
title: ps.title,
name: ps.name,
name: ps.name ? ps.name.trim() : undefined,
summary: ps.summary === undefined ? page.summary : ps.summary,
content: ps.content,
variables: ps.variables,
Expand Down

0 comments on commit 40630a6

Please sign in to comment.