Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset plugin #542

Draft
wants to merge 11 commits into
base: default
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
"node": ">= 14.17.0"
},
"dependencies": {
"abstract-blob-store": "^3.3.5",
"ajv": "^8.0.5",
"ajv-formats": "^2.0.2",
"avvio": "^8.0.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"busboy": "^1.6.0",
"cookie": "^0.5.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"escape-string-regexp": "^4.0.0",
"explain-error": "^1.0.4",
"express": "^4.17.1",
"fs-blob-store": "^6.0.0",
"has": "^1.0.3",
"helmet": "^6.0.0",
"htmlescape": "^1.1.1",
Expand All @@ -47,6 +50,7 @@
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.15",
"make-promises-safe": "^5.1.0",
"mime": "^3.0.0",
"minimist": "^1.2.5",
"mongoose": "^6.3.8",
"ms": "^2.1.2",
Expand Down Expand Up @@ -74,6 +78,7 @@
"devDependencies": {
"@tsconfig/node14": "^1.0.1",
"@types/bcryptjs": "^2.4.2",
"@types/busboy": "^1.5.0",
"@types/cookie": "^0.5.0",
"@types/cookie-parser": "^1.4.2",
"@types/cors": "^2.8.10",
Expand All @@ -85,6 +90,7 @@
"@types/json-merge-patch": "^0.0.8",
"@types/jsonwebtoken": "^8.5.1",
"@types/lodash": "^4.14.168",
"@types/mime": "^3.0.1",
"@types/ms": "^0.7.31",
"@types/node": "14",
"@types/node-fetch": "^2.5.8",
Expand Down
19 changes: 19 additions & 0 deletions src/Uwave.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { i18n } = require('./locale');

const models = require('./models');
const configStore = require('./plugins/configStore');
const assets = require('./plugins/assets');
const booth = require('./plugins/booth');
const chat = require('./plugins/chat');
const motd = require('./plugins/motd');
Expand All @@ -26,6 +27,8 @@ const waitlist = require('./plugins/waitlist');
const passport = require('./plugins/passport');
const migrations = require('./plugins/migrations');

const baseSchema = require('./schemas/base.json');

const DEFAULT_MONGO_URL = 'mongodb://localhost:27017/uwave';
const DEFAULT_REDIS_URL = 'redis://localhost:6379';

Expand Down Expand Up @@ -83,6 +86,10 @@ class UwaveServer extends EventEmitter {
// @ts-expect-error TS2564 Definitely assigned in a plugin
config;

/** @type {import('./plugins/assets').Assets} */
// @ts-expect-error TS2564 Definitely assigned in a plugin
assets;

/** @type {import('./plugins/history').HistoryRepository} */
// @ts-expect-error TS2564 Definitely assigned in a plugin
history;
Expand Down Expand Up @@ -173,7 +180,15 @@ class UwaveServer extends EventEmitter {

boot.use(models);
boot.use(migrations);

boot.use(configStore);
boot.use(async (uw) => {
uw.config.register(baseSchema['uw:key'], baseSchema);
});

boot.use(assets, {
basedir: '/tmp/u-wave-basedir',
});

boot.use(passport, {
secret: this.options.secret,
Expand All @@ -190,6 +205,10 @@ class UwaveServer extends EventEmitter {
});
boot.use(SocketServer.plugin);

boot.use(async (uw) => {
uw.express.use('/assets', uw.assets.middleware());
});

boot.use(acl);
boot.use(chat);
boot.use(motd);
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/now.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ async function getState(req) {
});
}

const baseConfig = await uw.config.get('u-wave:base');

const stateShape = {
motd,
user: user ? serializeUser(user) : null,
Expand Down Expand Up @@ -138,6 +140,8 @@ async function getState(req) {
state.playlists = state.playlists.map(serializePlaylist);
}

// TODO dynamically return all the public-facing config.
state.config = { 'u-wave:base': baseConfig };
return state;
}

Expand Down
75 changes: 73 additions & 2 deletions src/controllers/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

const { finished } = require('stream');
const { NotFound } = require('http-errors');
const busboy = require('busboy');
const toItemResponse = require('../utils/toItemResponse');

/**
Expand All @@ -26,7 +29,7 @@ async function getAllConfig(req) {
}

/**
* @type {import('../types').AuthenticatedController}
* @type {import('../types').AuthenticatedController<{ key: string }>}
*/
async function getConfig(req) {
const { config } = req.uwave;
Expand All @@ -44,7 +47,7 @@ async function getConfig(req) {
}

/**
* @type {import('../types').AuthenticatedController}
* @type {import('../types').AuthenticatedController<{ key: string }>}
*/
async function updateConfig(req) {
const { config } = req.uwave;
Expand All @@ -58,9 +61,77 @@ async function updateConfig(req) {
});
}

/**
* @param {import('ajv').SchemaObject} schema
* @param {string} path
* @returns {import('ajv').SchemaObject|null}
*/
function getPath(schema, path) {
const parts = path.split('.');
let descended = schema;
for (const part of parts) {
descended = descended.properties[part];
if (!descended) {
return null;
}
}
return descended;
}

/**
* @type {import('../types').AuthenticatedController<{ key: string }, {}, never>}
*/
async function uploadFile(req) {
const { config, assets } = req.uwave;
const { key } = req.params;

const combinedSchema = config.getSchema();
const schema = getPath(combinedSchema, key);
if (!schema) {
throw new NotFound('Config key does not exist');
}
if (schema.type !== 'string' || schema.format !== 'asset') {
throw new NotFound('Config key is not an asset');
}

const [content, meta] = await new Promise((resolve, reject) => {
const bb = busboy({ headers: req.headers });
bb.on('file', (name, file, info) => {
if (name !== 'file') {
return;
}

/** @type {Buffer[]} */
const chunks = [];
file.on('data', (chunk) => {
chunks.push(chunk);
});

finished(file, (err) => {
if (err) {
reject(err);
} else {
resolve([Buffer.concat(chunks), info]);
}
});
});
req.pipe(bb);
});

const path = await assets.store(meta.filename, content, {
category: 'config',
userID: req.user._id,
});

return toItemResponse({ path }, {
url: req.fullUrl,
});
}

module.exports = {
getServerTime,
getAllConfig,
getConfig,
updateConfig,
uploadFile,
};
41 changes: 41 additions & 0 deletions src/models/Asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const mongoose = require('mongoose');

const { Schema } = mongoose;
const { Types } = mongoose.Schema;

/**
* @typedef {object} LeanAsset
* @prop {import('mongodb').ObjectId} _id
* @prop {string} name
* @prop {string} path
* @prop {string} category
* @prop {import('mongodb').ObjectId} user
* @prop {Date} createdAt
* @prop {Date} updatedAt
*
* @typedef {mongoose.Document<LeanAsset["_id"], {}, LeanAsset> &
* LeanAsset} Asset
*/

/**
* @type {mongoose.Schema<Asset, mongoose.Model<Asset>>}
*/
const schema = new Schema({
name: { type: String, required: true },
path: { type: String, required: true },
category: { type: String, required: true },
user: {
type: Types.ObjectId,
ref: 'User',
required: true,
index: true,
},
}, {
collection: 'assets',
timestamps: true,
toJSON: { versionKey: false },
});

module.exports = schema;
4 changes: 4 additions & 0 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const migrationSchema = require('./Migration');
const playlistSchema = require('./Playlist');
const playlistItemSchema = require('./PlaylistItem');
const userSchema = require('./User');
const assetSchema = require('./Asset');

/**
* @typedef {import('./AclRole').AclRole} AclRole
Expand All @@ -20,6 +21,7 @@ const userSchema = require('./User');
* @typedef {import('./Playlist').Playlist} Playlist
* @typedef {import('./PlaylistItem').PlaylistItem} PlaylistItem
* @typedef {import('./User').User} User
* @typedef {import('./Asset').Asset} Asset
* @typedef {{
* AclRole: import('mongoose').Model<AclRole, {}, {}>,
* Authentication: import('mongoose').Model<Authentication, {}, {}>,
Expand All @@ -30,6 +32,7 @@ const userSchema = require('./User');
* Playlist: import('mongoose').Model<Playlist, {}, {}>,
* PlaylistItem: import('mongoose').Model<PlaylistItem, {}, {}>,
* User: import('mongoose').Model<User, {}, {}>,
* Asset: import('mongoose').Model<Asset, {}, {}>,
* }} Models
*/

Expand All @@ -47,6 +50,7 @@ async function models(uw) {
Playlist: uw.mongo.model('Playlist', playlistSchema),
PlaylistItem: uw.mongo.model('PlaylistItem', playlistItemSchema),
User: uw.mongo.model('User', userSchema),
Asset: uw.mongo.model('Asset', assetSchema),
};
}

Expand Down
20 changes: 20 additions & 0 deletions src/modules.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declare module 'fs-blob-store' {
import { AbstractBlobStore } from 'abstract-blob-store';

type Key = { key: string };
type CreateCallback = (error: Error | null, metadata: Key) => void;

class FsBlobStore implements AbstractBlobStore {
constructor(basedir: string)

createWriteStream(opts: Key, callback: CreateCallback): NodeJS.WriteStream

createReadStream(opts: Key): NodeJS.ReadStream

exists(opts: Key, callback: ExistsCallback): void

remove(opts: Key, callback: RemoveCallback): void
}

export = FsBlobStore;
}
Loading