Skip to content

Commit

Permalink
Merge pull request #1 from sarthakpranesh/switchToLocalStore
Browse files Browse the repository at this point in the history
chore: adding mocked S3 bucket for local filestore
  • Loading branch information
SebastianStehle authored Mar 11, 2024
2 parents d623946 + 6d123f7 commit 63edb78
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# port you want the server to use, defaults to 8080
SERVER_PORT=8001

# you want to use gcp or local filesystem
# gcp -> google cloud storage, local -> local filesystem
# defaults to gcp
STORAGE_TYPE=local

# if storage type is gcp/not set, then uncomment below and add the bucket name
# defaults to "athene-diagram-files"
# STORAGE_GCP_BUCKET_NAME=athene-diagram-files
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]]
}
}
}
}
})();
15 changes: 11 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import 'dotenv/config'
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.STORAGE_TYPE === "local") {
storageBucket = fileStore
} else {
const storageClient = new Storage();
storageBucket = storageClient.bucket(process.env.STORAGE_GCP_BUCKET_NAME || 'athene-diagram-files');
}

const server = Server.configure({
extensions: [
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@hocuspocus/provider": "^2.7.1",
"@hocuspocus/server": "^2.7.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express-ws": "^5.0.2",
"short-unique-id": "^5.0.3",
Expand Down

0 comments on commit 63edb78

Please sign in to comment.