Skip to content

Commit

Permalink
Merge pull request #31 from DIG-Network/release/v0.0.1-alpha.34
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.34
  • Loading branch information
MichaelTaylor3D authored Sep 22, 2024
2 parents afc1eb0 + cac5a00 commit fad49b7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.34](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.33...v0.0.1-alpha.34) (2024-09-22)


### Bug Fixes

* add missing head file upload ([a56a316](https://github.com/DIG-Network/dig-propagation-server/commit/a56a316690af719c7714bc4fd340a64a4837edce))

### [0.0.1-alpha.33](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.32...v0.0.1-alpha.33) (2024-09-22)

### [0.0.1-alpha.32](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.31...v0.0.1-alpha.32) (2024-09-22)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dig-propagation-server",
"version": "0.0.1-alpha.33",
"version": "0.0.1-alpha.34",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
42 changes: 42 additions & 0 deletions src/controllers/merkleTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getStorageLocation } from "../utils/storage";
import tmp from "tmp";
import { PassThrough } from "stream";
import NodeCache from "node-cache";
import { generateNonce, validateNonce } from "../utils/nonce";

const digFolderPath = getStorageLocation();
const streamPipeline = promisify(require("stream").pipeline);
Expand Down Expand Up @@ -164,6 +165,43 @@ export const startUploadSession = async (
}
};

/**
* Handle the HEAD request for /upload/{storeId}/{sessionId}/{filename}
* Returns a nonce in the headers for file upload.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
export const generateFileNonce = async (req: Request, res: Response): Promise<void> => {
try {
const { storeId, sessionId, filename } = req.params;

if (!storeId || !sessionId || !filename) {
throw new HttpError(400, "Missing required parameters.");
}

// Construct the path for the upload session
const sessionPath = path.join(digFolderPath, "uploads", storeId, sessionId);

// Check if the session directory exists
if (!fs.existsSync(sessionPath)) {
throw new HttpError(404, "Upload session not found.");
}

// Generate a nonce for the file
const nonce = generateNonce(`${storeId}_${sessionId}_${filename}`);

// Set the nonce in the headers
res.setHeader("x-nonce", nonce);

// Return 200 status with no body, as per HEAD request specification
res.status(200).end();
} catch (error: any) {
console.error("Error generating nonce:", error);
const statusCode = error instanceof HttpError ? error.statusCode : 500;
res.status(statusCode).end();
}
};

/**
* Upload a file to a DataStore (PUT /upload/{storeId}/{sessionId}/{filename})
* Each session has a unique session folder under the DataStore.
Expand Down Expand Up @@ -199,6 +237,10 @@ export const uploadFile = async (
publicKey
);

if (validateNonce(`${storeId}_${sessionId}_${filename}`, nonce)) {
throw new HttpError(401, "Invalid nonce.");
}

if (!isSignatureValid) {
console.log("Key ownership signature is invalid.");
throw new HttpError(401, "Invalid key ownership signature.");
Expand Down
13 changes: 8 additions & 5 deletions src/routes/storeRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
startUploadSession,
uploadFile,
commitUpload,
abortUpload
abortUpload,
generateFileNonce
} from "../controllers/merkleTreeController";

import { setMnemonic } from "../controllers/configController";
import { verifyMnemonic } from "../middleware/verifyMnemonic";
import { subscribeToStore, unsubscribeToStore } from "../controllers/storeController";


const router = express.Router();

router.post("/unsubscribe", express.json(), unsubscribeToStore);
Expand All @@ -25,13 +25,16 @@ router.head("/:storeId", verifyMnemonic, headStore);
router.post("/upload/:storeId", verifyMnemonic, startUploadSession);

// Upload a file to a store's session (PUT /upload/{storeId}/{sessionId}/{filename})
router.put("/upload/:storeId/:sessionId/:filename", verifyMnemonic, uploadFile);
router.head("/upload/:storeId/:sessionId/:filename", generateFileNonce);

// Upload a file to a store's session (PUT /upload/{storeId}/{sessionId}/{filename})
router.put("/upload/:storeId/:sessionId/:filename", uploadFile);

// Commit an upload (POST /commit/{storeId}/{sessionId})
router.post("/commit/:storeId/:sessionId", verifyMnemonic, commitUpload);
router.post("/commit/:storeId/:sessionId", commitUpload);

// Abort an upload session (POST /abort/{storeId}/{sessionId})
router.post("/abort/:storeId/:sessionId", verifyMnemonic, abortUpload);
router.post("/abort/:storeId/:sessionId", abortUpload);


export { router as storeRoutes };

0 comments on commit fad49b7

Please sign in to comment.