Skip to content

Commit

Permalink
Merge pull request #47 from badgeteam/36-rest-api-draft-file-download…
Browse files Browse the repository at this point in the history
…-and-upload

36 rest api draft file download and upload
  • Loading branch information
edwinm authored Jan 5, 2025
2 parents f04d4e8 + 23a8b6e commit d2c9eec
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@ dist
.pnp.*
/backup/data-backup.sql
/backup/

# data dir
data
!data/.gitkeep
Empty file added data/.gitkeep
Empty file.
128 changes: 125 additions & 3 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"express": "^4.21.2",
"jose": "^5.9.6",
"moment": "^2.30.1",
"multer": "^1.4.5-lts.1",
"pg": "^8.11.5",
"pino-http": "^10.1.0",
"pm2": "^5.4.0",
Expand All @@ -41,6 +42,7 @@
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/multer": "^1.4.12",
"@types/node": "^20.17.6",
"@types/pg": "^8.11.5",
"@types/supertest": "^6.0.2",
Expand Down
23 changes: 12 additions & 11 deletions public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@
},
"/api/v3/apps/{slug}/draft/files/{filePath}": {
"post": {
"operationId": "WriteFile",
"operationId": "WriteDraftFile",
"responses": {
"204": {
"description": "No content"
Expand Down Expand Up @@ -1264,16 +1264,16 @@
"requestBody": {
"required": true,
"content": {
"application/json": {
"multipart/form-data": {
"schema": {
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/components/schemas/Uint8Array"
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
]
},
"required": ["file"]
}
}
}
Expand All @@ -1287,7 +1287,8 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Uint8Array"
"type": "string",
"format": "byte"
}
}
}
Expand Down Expand Up @@ -1318,7 +1319,7 @@
},
"/api/v3/apps/{slug}/draft/metadata": {
"patch": {
"operationId": "ChangeAppMetadata",
"operationId": "ChangeDraftAppMetadata",
"responses": {
"204": {
"description": "No content"
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { config } from "dotenv";
import * as path from "node:path";

config();
export const EXPRESS_PORT = 8081;
Expand All @@ -8,3 +9,4 @@ export const POSTGRES_PASSWORD = process.env.POSTGRES_PASSWORD;
export const POSTGRES_HOST = process.env.DB_HOST;
export const POSTGRES_PORT = 5432;
export const NODE_ENV = process.env.NODE_ENV;
export const DATA_DIR = process.env.DATA_DIR || "data";
40 changes: 33 additions & 7 deletions src/controllers/private-rest.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Body, Delete, Get, Patch, Path, Post, Route, Tags } from "tsoa";
import {
Body,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Tags,
UploadedFile,
} from "tsoa";
import { BadgeHubData } from "@domain/BadgeHubData";
import { PostgreSQLBadgeHubMetadata } from "@db/PostgreSQLBadgeHubMetadata";
import type { ProjectSlug } from "@domain/readModels/app/Project";
import type { DBInsertUser, DBUser } from "@db/models/app/DBUser";
import type { DBInsertProject } from "@db/models/app/DBProject";
import type { DBInsertAppMetadataJSON } from "@db/models/app/DBAppMetadataJSON";
import { NodeFSBadgeHubFiles } from "@fs/NodeFSBadgeHubFiles";
import express from "express";
import { Readable } from "node:stream";

interface UserProps extends Omit<DBInsertUser, "id"> {}

interface ProjectProps extends Omit<DBInsertProject, "slug"> {}

interface ProjectPropsPartial extends Partial<ProjectProps> {}

interface DbInsertAppMetadataJSONPartial
extends Partial<DBInsertAppMetadataJSON> {}

Expand Down Expand Up @@ -71,19 +86,25 @@ export class PrivateRestController {
* Upload a file to the latest draft version of the project.
*/
@Post("/apps/{slug}/draft/files/{filePath}")
public async writeFile(
public async writeDraftFile(
@Path() slug: string,
@Path() filePath: string,
@Body() fileContent: string | Uint8Array
@UploadedFile() file: Express.Multer.File
): Promise<void> {
await this.badgeHubData.writeDraftFile(slug, filePath, fileContent);
await this.badgeHubData.writeDraftFile(slug, filePath, {
mimetype: file.mimetype,
fileContent: file.buffer,
directory: file.destination,
fileName: file.filename,
size: file.size,
});
}

/**
* Change the metadata of the latest draft version of the project.
*/
@Patch("/apps/{slug}/draft/metadata")
public async changeAppMetadata(
public async changeDraftAppMetadata(
@Path() slug: string,
@Body() appMetadataChanges: DbInsertAppMetadataJSONPartial
): Promise<void> {
Expand All @@ -97,8 +118,13 @@ export class PrivateRestController {
public async getDraftFile(
@Path() slug: string,
@Path() filePath: string
): Promise<Uint8Array> {
return await this.badgeHubData.getFileContents(slug, "draft", filePath);
): Promise<Readable> {
const fileContents = await this.badgeHubData.getFileContents(
slug,
"draft",
filePath
);
return Readable.from(fileContents);
}

/**
Expand Down
Loading

0 comments on commit d2c9eec

Please sign in to comment.