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

chore: adding mocked S3 bucket for local filestore #1

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
node_modules

# Output files
dist
dist

# local filestore
localFileStore
47 changes: 47 additions & 0 deletions fileStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

import EventEmitter from 'node:events';
import * as fs from 'node:fs';

export default (() => {
const base = './localFileStore/'
const metaPath = `${base}_meta.json`
if (fs.existsSync(base) === false) {
fs.mkdirSync(base)
}
if (fs.existsSync(metaPath) === false) {
fs.writeFileSync(metaPath, JSON.stringify({}))
}
return {
file: (filePath: string) => {
const fullPath = `${base}${filePath}`
return {
exists: () => {
return [fs.existsSync(fullPath)]
},
download: async () => {
return [new Uint8Array(fs.readFileSync(fullPath))]
},
createWriteStream: () => {
return fs.createWriteStream(fullPath)
},
createReadStream: () => {
return fs.createReadStream(fullPath)
},
save: (fileContent: string, meta?: undefined | any) => {
if (typeof meta === 'object') {
const rawMeta = fs.readFileSync(metaPath);
const _meta = JSON.parse(rawMeta.toString())
_meta[fullPath] = meta.metadata
fs.writeFileSync(metaPath, JSON.stringify(_meta))
}
fs.writeFileSync(fullPath, fileContent)
},
getMetadata: () => {
const rawMeta = fs.readFileSync(metaPath);
const _meta = JSON.parse(rawMeta.toString())
return [_meta[fullPath]]
}
}
}
}
})();
12 changes: 9 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Database } from '@hocuspocus/extension-database';
import { Logger } from '@hocuspocus/extension-logger';
import { Server } from '@hocuspocus/server';
import { Storage } from '@google-cloud/storage';
import { Bucket, Storage } from '@google-cloud/storage';
import { utils } from './stream-helper'
import cors from 'cors';
import express from 'express';
import expressWebsockets from 'express-ws';
import ShortUniqueId from 'short-unique-id';
import fileStore from './fileStore';

const uid = new ShortUniqueId({ length: 20 });

const serverPort = parseInt(process.env.SERVER_PORT || '8080');
const storageClient = new Storage();
const storageBucket = storageClient.bucket(process.env.BUCKET_NAME || 'athene-diagram-files');
let storageBucket: Bucket | typeof fileStore;
if (process.env.BUCKET_NAME) {
const storageClient = new Storage();
storageBucket = storageClient.bucket(process.env.BUCKET_NAME);
} else {
storageBucket = fileStore
}

const server = Server.configure({
extensions: [
Expand Down