Procházet zdrojové kódy

storage-node-v2: Add static files service.

Shamil Gadelshin před 3 roky
rodič
revize
3b500b487c

+ 8 - 10
storage-node-v2/src/commands/multihash.ts

@@ -1,21 +1,19 @@
-import {Command, flags} from '@oclif/command'
+import { Command, flags } from '@oclif/command'
 import { hashFile } from '../services/hashing'
 
 export default class Multihash extends Command {
   static description = 'Creates a multihash (blake3) for a file.'
 
   static flags = {
-    help: flags.help({char: 'h'}),
-    file: flags.string(
-      {
-        char: 'f',
-        required: true,
-        description: 'Path for a hashing file.'
-      }
-      ),
+    help: flags.help({ char: 'h' }),
+    file: flags.string({
+      char: 'f',
+      required: true,
+      description: 'Path for a hashing file.',
+    }),
   }
 
-  async run() {
+  async run(): Promise<void> {
     const { flags } = this.parse(Multihash)
 
     console.log(`Hashing ${flags.file}`)

+ 11 - 11
storage-node-v2/src/services/hashing.ts

@@ -1,23 +1,23 @@
-import * as  multihash from 'multihashes'
+import * as multihash from 'multihashes'
 import fs from 'fs'
 import { createHash } from 'blake3'
 
 export function hashFile(filename: string): Promise<string> {
-  let fileStream = fs.createReadStream(filename).pipe(createHash())
+  const fileStream = fs.createReadStream(filename).pipe(createHash())
 
   return new Promise((resolve, reject) => {
-    let hash: Uint8Array;
+    let hash: Uint8Array
     fileStream.on('data', function (chunk) {
-      hash = chunk;
-    });
+      hash = chunk
+    })
     fileStream.on('end', function () {
       const encoded = multihash.encode(hash, 'blake3')
       const result = multihash.toB58String(encoded)
 
-      resolve(result);
-    });
+      resolve(result)
+    })
     fileStream.on('error', function (err) {
-      reject(err);
-    });
-  });
-}
+      reject(err)
+    })
+  })
+}

+ 6 - 8
storage-node-v2/src/services/webApi/controllers/publicApi.ts

@@ -2,10 +2,8 @@ import * as express from 'express'
 import { acceptPendingDataObjects } from '../../runtime/extrinsics'
 import { getAlicePair } from '../../runtime/api'
 import { hashFile } from '../../../services/hashing'
-import fs from 'fs';
-const fsPromises = fs.promises;
-
-//import * as multer from '@types/multer'
+import fs from 'fs'
+const fsPromises = fs.promises
 
 // TODO: test api connection?
 // TODO: error handling
@@ -51,15 +49,15 @@ export async function upload(
   }
 }
 
-function getFileObject(req: express.Request): Express.Multer.File{
+function getFileObject(req: express.Request): Express.Multer.File {
   if (req.file) {
     return req.file
   }
 
-  const files = req.files as Express.Multer.File[];
+  const files = req.files as Express.Multer.File[]
   if (files && files.length > 0) {
-     return files[0]
+    return files[0]
   }
 
   throw new Error('No file uploaded')
-}
+}

+ 3 - 3
storage-node-v2/src/services/webApi/server.ts

@@ -4,8 +4,6 @@ import cors from 'cors'
 import { Express } from 'express-serve-static-core'
 import * as OpenApiValidator from 'express-openapi-validator'
 
-
-
 // TODO: custom errors (including validation errors)
 
 export async function createServer(
@@ -17,10 +15,12 @@ export async function createServer(
   const spec = path.join(__dirname, './../../api-spec/openapi.yaml')
 
   if (devMode) {
-    // TODO: localhost only?
     server.use('/spec', express.static(spec))
   }
 
+  // TODO: check path
+  server.use('/files', express.static(uploadsDir))
+
   server.use(
     OpenApiValidator.middleware({
       apiSpec: spec,