diff --git a/.eslintignore b/.eslintignore index 62e6a4a8b3..2defed4a95 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1490,12 +1490,6 @@ packages/server/src/controllers/api/OAuthController.js.map packages/server/src/controllers/api/SessionController.d.ts packages/server/src/controllers/api/SessionController.js packages/server/src/controllers/api/SessionController.js.map -packages/server/src/controllers/api/UserController.d.ts -packages/server/src/controllers/api/UserController.js -packages/server/src/controllers/api/UserController.js.map -packages/server/src/controllers/api/UserController.test.d.ts -packages/server/src/controllers/api/UserController.test.js -packages/server/src/controllers/api/UserController.test.js.map packages/server/src/controllers/factory.d.ts packages/server/src/controllers/factory.js packages/server/src/controllers/factory.js.map @@ -1511,9 +1505,6 @@ packages/server/src/controllers/index/LoginController.js.map packages/server/src/controllers/index/NotificationController.d.ts packages/server/src/controllers/index/NotificationController.js packages/server/src/controllers/index/NotificationController.js.map -packages/server/src/controllers/index/ProfileController.d.ts -packages/server/src/controllers/index/ProfileController.js -packages/server/src/controllers/index/ProfileController.js.map packages/server/src/controllers/index/UserController.d.ts packages/server/src/controllers/index/UserController.js packages/server/src/controllers/index/UserController.js.map @@ -1574,6 +1565,9 @@ packages/server/src/models/SessionModel.js.map packages/server/src/models/UserModel.d.ts packages/server/src/models/UserModel.js packages/server/src/models/UserModel.js.map +packages/server/src/models/UserModel.test.d.ts +packages/server/src/models/UserModel.test.js +packages/server/src/models/UserModel.test.js.map packages/server/src/models/factory.d.ts packages/server/src/models/factory.js packages/server/src/models/factory.js.map @@ -1622,12 +1616,6 @@ packages/server/src/routes/index/logout.js.map packages/server/src/routes/index/notifications.d.ts packages/server/src/routes/index/notifications.js packages/server/src/routes/index/notifications.js.map -packages/server/src/routes/index/profile.d.ts -packages/server/src/routes/index/profile.js -packages/server/src/routes/index/profile.js.map -packages/server/src/routes/index/user.d.ts -packages/server/src/routes/index/user.js -packages/server/src/routes/index/user.js.map packages/server/src/routes/index/users.d.ts packages/server/src/routes/index/users.js packages/server/src/routes/index/users.js.map diff --git a/.gitignore b/.gitignore index 5e6f7a5017..e6f7d58be7 100644 --- a/.gitignore +++ b/.gitignore @@ -1479,12 +1479,6 @@ packages/server/src/controllers/api/OAuthController.js.map packages/server/src/controllers/api/SessionController.d.ts packages/server/src/controllers/api/SessionController.js packages/server/src/controllers/api/SessionController.js.map -packages/server/src/controllers/api/UserController.d.ts -packages/server/src/controllers/api/UserController.js -packages/server/src/controllers/api/UserController.js.map -packages/server/src/controllers/api/UserController.test.d.ts -packages/server/src/controllers/api/UserController.test.js -packages/server/src/controllers/api/UserController.test.js.map packages/server/src/controllers/factory.d.ts packages/server/src/controllers/factory.js packages/server/src/controllers/factory.js.map @@ -1500,9 +1494,6 @@ packages/server/src/controllers/index/LoginController.js.map packages/server/src/controllers/index/NotificationController.d.ts packages/server/src/controllers/index/NotificationController.js packages/server/src/controllers/index/NotificationController.js.map -packages/server/src/controllers/index/ProfileController.d.ts -packages/server/src/controllers/index/ProfileController.js -packages/server/src/controllers/index/ProfileController.js.map packages/server/src/controllers/index/UserController.d.ts packages/server/src/controllers/index/UserController.js packages/server/src/controllers/index/UserController.js.map @@ -1563,6 +1554,9 @@ packages/server/src/models/SessionModel.js.map packages/server/src/models/UserModel.d.ts packages/server/src/models/UserModel.js packages/server/src/models/UserModel.js.map +packages/server/src/models/UserModel.test.d.ts +packages/server/src/models/UserModel.test.js +packages/server/src/models/UserModel.test.js.map packages/server/src/models/factory.d.ts packages/server/src/models/factory.js packages/server/src/models/factory.js.map @@ -1611,12 +1605,6 @@ packages/server/src/routes/index/logout.js.map packages/server/src/routes/index/notifications.d.ts packages/server/src/routes/index/notifications.js packages/server/src/routes/index/notifications.js.map -packages/server/src/routes/index/profile.d.ts -packages/server/src/routes/index/profile.js -packages/server/src/routes/index/profile.js.map -packages/server/src/routes/index/user.d.ts -packages/server/src/routes/index/user.js -packages/server/src/routes/index/user.js.map packages/server/src/routes/index/users.d.ts packages/server/src/routes/index/users.js packages/server/src/routes/index/users.js.map diff --git a/packages/lib/SyncTargetJoplinServer.ts b/packages/lib/SyncTargetJoplinServer.ts index 2b09f92b66..c756f53020 100644 --- a/packages/lib/SyncTargetJoplinServer.ts +++ b/packages/lib/SyncTargetJoplinServer.ts @@ -52,15 +52,15 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget { } static async checkConfig(options: FileApiOptions) { - const fileApi = await SyncTargetJoplinServer.newFileApi_(options); - fileApi.requestRepeatCount_ = 0; - const output = { ok: false, errorMessage: '', }; try { + const fileApi = await SyncTargetJoplinServer.newFileApi_(options); + fileApi.requestRepeatCount_ = 0; + const result = await fileApi.stat(''); if (!result) throw new Error(`Sync directory not found: "${options.directory()}" on server "${options.path()}"`); output.ok = true; diff --git a/packages/server/src/controllers/api/FileController.ts b/packages/server/src/controllers/api/FileController.ts deleted file mode 100644 index 41f8e450e8..0000000000 --- a/packages/server/src/controllers/api/FileController.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { File } from '../../db'; -import BaseController from '../BaseController'; -import { ErrorNotFound } from '../../utils/errors'; -import { Pagination } from '../../models/utils/pagination'; -import { PaginatedFiles } from '../../models/FileModel'; -import { ChangePagination, PaginatedChanges } from '../../models/ChangeModel'; - -export default class FileController extends BaseController { - - public async getFile(sessionId: string, fileId: string): Promise { - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - const file: File = await fileModel.entityFromItemId(fileId); - const loadedFile = await fileModel.load(file.id); - if (!loadedFile) throw new ErrorNotFound(); - return fileModel.toApiOutput(loadedFile); - } - - public async getFileContent(sessionId: string, fileId: string): Promise { - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - let file: File = await fileModel.entityFromItemId(fileId); - file = await fileModel.loadWithContent(file.id); - if (!file) throw new ErrorNotFound(); - return file; - } - - public async patchFile(sessionId: string, fileId: string, file: File): Promise { - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - const existingFile: File = await fileModel.entityFromItemId(fileId); - const newFile = fileModel.fromApiInput(file); - newFile.id = existingFile.id; - return fileModel.toApiOutput(await fileModel.save(newFile)); - } - - public async putFileContent(sessionId: string, fileId: string, content: Buffer): Promise { - if (!content) content = Buffer.alloc(0); - - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - const file: File = await fileModel.entityFromItemId(fileId, { mustExist: false }); - file.content = content; - return fileModel.toApiOutput(await fileModel.save(file, { validationRules: { mustBeFile: true } })); - } - - public async deleteFileContent(sessionId: string, fileId: string): Promise { - await this.putFileContent(sessionId, fileId, null); - } - - public async getChildren(sessionId: string, dirId: string, pagination: Pagination): Promise { - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - const parent: File = await fileModel.entityFromItemId(dirId); - return fileModel.toApiOutput(await fileModel.childrens(parent.id, pagination)); - } - - public async postChild(sessionId: string, fileId: string, child: File): Promise { - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - const parent: File = await fileModel.entityFromItemId(fileId); - child = fileModel.fromApiInput(child); - child.parent_id = parent.id; - return fileModel.toApiOutput(await fileModel.save(child)); - } - - public async deleteFile(sessionId: string, fileId: string): Promise { - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - try { - const file: File = await fileModel.entityFromItemId(fileId, { mustExist: false }); - if (!file.id) return; - await fileModel.delete(file.id); - } catch (error) { - if (error instanceof ErrorNotFound) { - // That's ok - a no-op - } else { - throw error; - } - } - } - - public async getDelta(sessionId: string, dirId: string, pagination: ChangePagination): Promise { - const user = await this.initSession(sessionId); - const fileModel = this.models.file({ userId: user.id }); - const dir: File = await fileModel.entityFromItemId(dirId, { mustExist: true }); - const changeModel = this.models.change({ userId: user.id }); - return changeModel.byDirectoryId(dir.id, pagination); - } - -} diff --git a/packages/server/src/controllers/factory.ts b/packages/server/src/controllers/factory.ts index 9afcbe2f03..a1e80f5794 100644 --- a/packages/server/src/controllers/factory.ts +++ b/packages/server/src/controllers/factory.ts @@ -1,5 +1,4 @@ import { Models } from '../models/factory'; -import FileController from './api/FileController'; // import OAuthController from './api/OAuthController'; import SessionController from './api/SessionController'; import IndexLoginController from './index/LoginController'; @@ -16,10 +15,6 @@ export class Controllers { this.models_ = models; } - public apiFile() { - return new FileController(this.models_); - } - // public oauth() { // return new OAuthController(this.models_); // } diff --git a/packages/server/src/middleware/ownerHandler.ts b/packages/server/src/middleware/ownerHandler.ts index 2dc0c8bfc6..f3bb403fb0 100644 --- a/packages/server/src/middleware/ownerHandler.ts +++ b/packages/server/src/middleware/ownerHandler.ts @@ -1,17 +1,8 @@ import { AppContext, KoaNext } from '../utils/types'; -import { isApiRequest, contextSessionId } from '../utils/requestUtils'; -import Logger from '@joplin/lib/Logger'; - -const logger = Logger.create('ownerHandler'); +import { contextSessionId } from '../utils/requestUtils'; export default async function(ctx: AppContext, next: KoaNext): Promise { - try { - if (isApiRequest(ctx)) return next(); - const sessionId = contextSessionId(ctx); - ctx.owner = await ctx.models.session().sessionUser(sessionId); - } catch (error) { - logger.error(error); - } - + const sessionId = contextSessionId(ctx, false); + if (sessionId) ctx.owner = await ctx.models.session().sessionUser(sessionId); return next(); } diff --git a/packages/server/src/routes/index/files.ts b/packages/server/src/routes/index/files.ts index a802e35646..306ac65128 100644 --- a/packages/server/src/routes/index/files.ts +++ b/packages/server/src/routes/index/files.ts @@ -13,7 +13,10 @@ const route: Route = { if (!path.link) { return ctx.controllers.indexFiles().getIndex(sessionId, path.id, ctx.query); } else if (path.link === 'content') { - const file: File = await ctx.controllers.apiFile().getFileContent(sessionId, path.id); + const fileModel = ctx.models.file({ userId: ctx.owner.id }); + let file: File = await fileModel.entityFromItemId(path.id); + file = await fileModel.loadWithContent(file.id); + if (!file) throw new ErrorNotFound(); return respondWithFileContent(ctx.response, file); } diff --git a/packages/server/src/utils/requestUtils.ts b/packages/server/src/utils/requestUtils.ts index 12928fc55a..81a3038385 100644 --- a/packages/server/src/utils/requestUtils.ts +++ b/packages/server/src/utils/requestUtils.ts @@ -48,9 +48,11 @@ export function headerSessionId(headers: any): string { return headers['x-api-auth'] ? headers['x-api-auth'] : ''; } -export function contextSessionId(ctx: AppContext): string { +export function contextSessionId(ctx: AppContext, throwIfNotFound = true): string { + if (ctx.headers['x-api-auth']) return ctx.headers['x-api-auth']; + const id = ctx.cookies.get('sessionId'); - if (!id) throw new ErrorForbidden('Invalid or missing session'); + if (!id && throwIfNotFound) throw new ErrorForbidden('Invalid or missing session'); return id; }