mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Server: Removed the need for file controller
This commit is contained in:
parent
03dc1bbfe1
commit
3c5ac1ecc5
@ -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
|
||||
|
18
.gitignore
vendored
18
.gitignore
vendored
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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<File> {
|
||||
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<File> {
|
||||
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<File> {
|
||||
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<any> {
|
||||
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<any> {
|
||||
await this.putFileContent(sessionId, fileId, null);
|
||||
}
|
||||
|
||||
public async getChildren(sessionId: string, dirId: string, pagination: Pagination): Promise<PaginatedFiles> {
|
||||
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<File> {
|
||||
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<void> {
|
||||
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<PaginatedChanges> {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -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_);
|
||||
// }
|
||||
|
@ -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<void> {
|
||||
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();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user