From 302eba0d898e38dca14739486441c27c0be6c62f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 22 Apr 2021 10:55:28 +0200 Subject: Add data directory for plugins and some helpers --- server/lib/plugins/plugin-helpers-builder.ts | 44 +++++++++++++++++++--------- server/lib/plugins/plugin-manager.ts | 5 +++- 2 files changed, 34 insertions(+), 15 deletions(-) (limited to 'server/lib/plugins') diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts index cbd849742..d57c69ef0 100644 --- a/server/lib/plugins/plugin-helpers-builder.ts +++ b/server/lib/plugins/plugin-helpers-builder.ts @@ -1,19 +1,22 @@ -import { PeerTubeHelpers } from '@server/types/plugins' -import { sequelizeTypescript } from '@server/initializers/database' +import * as express from 'express' +import { join } from 'path' import { buildLogger } from '@server/helpers/logger' -import { VideoModel } from '@server/models/video/video' +import { CONFIG } from '@server/initializers/config' import { WEBSERVER } from '@server/initializers/constants' -import { ServerModel } from '@server/models/server/server' +import { sequelizeTypescript } from '@server/initializers/database' +import { AccountModel } from '@server/models/account/account' +import { AccountBlocklistModel } from '@server/models/account/account-blocklist' import { getServerActor } from '@server/models/application/application' -import { addServerInBlocklist, removeServerFromBlocklist, addAccountInBlocklist, removeAccountFromBlocklist } from '../blocklist' +import { ServerModel } from '@server/models/server/server' import { ServerBlocklistModel } from '@server/models/server/server-blocklist' -import { AccountModel } from '@server/models/account/account' -import { VideoBlacklistCreate } from '@shared/models' -import { blacklistVideo, unblacklistVideo } from '../video-blacklist' +import { VideoModel } from '@server/models/video/video' import { VideoBlacklistModel } from '@server/models/video/video-blacklist' -import { AccountBlocklistModel } from '@server/models/account/account-blocklist' -import { getServerConfig } from '../config' import { MPlugin } from '@server/types/models' +import { PeerTubeHelpers } from '@server/types/plugins' +import { VideoBlacklistCreate } from '@shared/models' +import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' +import { getServerConfig } from '../config' +import { blacklistVideo, unblacklistVideo } from '../video-blacklist' function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { const logger = buildPluginLogger(npmName) @@ -27,7 +30,9 @@ function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHel const moderation = buildModerationHelpers() - const plugin = buildPluginRelatedHelpers(pluginModel) + const plugin = buildPluginRelatedHelpers(pluginModel, npmName) + + const user = buildUserHelpers() return { logger, @@ -36,7 +41,8 @@ function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHel config, moderation, plugin, - server + server, + user } } @@ -145,8 +151,18 @@ function buildConfigHelpers () { } } -function buildPluginRelatedHelpers (plugin: MPlugin) { +function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) { + return { + getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`, + + getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`, + + getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName) + } +} + +function buildUserHelpers () { return { - getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/` + getAuthUser: (res: express.Response) => res.locals.oauth?.token?.User } } diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index ae05af721..ba9814383 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -1,7 +1,7 @@ import decache from 'decache' import * as express from 'express' import { createReadStream, createWriteStream } from 'fs' -import { outputFile, readJSON } from 'fs-extra' +import { ensureDir, outputFile, readJSON } from 'fs-extra' import { basename, join } from 'path' import { MOAuthTokenUser, MUser } from '@server/types/models' import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model' @@ -428,6 +428,9 @@ export class PluginManager implements ServerHook { } const { registerOptions, registerStore } = this.getRegisterHelpers(npmName, plugin) + + await ensureDir(registerOptions.peertubeHelpers.plugin.getDataDirectoryPath()) + library.register(registerOptions) .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) -- cgit v1.2.3