Compare commits

...

10 Commits

Author SHA1 Message Date
ogomez-at-wiris ccee09189a chore: Update beatstar version 2022-02-28 06:49:58 +01:00
ogomez-at-wiris ca989d7a23 feat: add global functions like versions 2022-02-27 12:06:03 +01:00
ogomez-at-wiris 434a54b038 add pack upload file 2022-02-27 07:40:05 +01:00
ogomez-at-wiris 5dabcc4649 feat: Add pack upload functionality 2022-02-27 07:39:21 +01:00
ogomez-at-wiris ddbd9e8303 feat: Add levels to pack 2022-02-25 05:07:15 +01:00
ogomez92 4451979a47 feat: add lint script 2022-02-25 04:51:24 +01:00
root 8d105f07bc chore: Improve formatting 2022-02-25 04:48:23 +01:00
ogomez-at-wiris 673db7a2e7 fix: Remove size from pack interface 2022-02-25 04:46:31 +01:00
ogomez-at-wiris 30b2a7bfdd feat: Remove size from packs 2022-02-25 04:44:37 +01:00
ogomez-at-wiris 0b659db2c3 feat Zip up the pack 2022-02-25 02:44:44 +01:00
11 changed files with 135 additions and 26 deletions

View File

@ -7,13 +7,16 @@
"license": "MIT",
"scripts": {
"dev": "nodemon src/server.ts",
"build": "rm -rf build/ && prettier --write src && tsc"
"lint": "prettier --write src",
"build": "rm -rf build/ && tsc"
},
"dependencies": {
"@nodelib/fs.walk": "^1.2.8",
"@types/express-fileupload": "^1.2.2",
"@types/multistream": "^2.1.2",
"axios": "^0.26.0",
"express": "^4.17.3",
"express-fileupload": "^1.3.1",
"express-zip": "^3.0.0",
"morgan": "^1.10.0",
"nodemon": "^2.0.15",

14
src/global_service.ts Normal file
View File

@ -0,0 +1,14 @@
import { Request, Response, NextFunction, response } from "express";
export default class GlobalService {
public static getVersions = async (
req: Request,
res: Response,
next: NextFunction
) => {
return res.status(200).json({
beat: '2022.02.28',
});
};
}

View File

@ -1,6 +1,6 @@
interface File {
path: string;
name: string;
}
export default File;
interface File {
path: string;
name: string;
}
export default File;

View File

@ -1,9 +1,9 @@
import File from './file';
import File from "./file";
interface Pack {
name: string;
size: number;
bpm: string;
files: File[];
levels: number;
}
export default Pack;

View File

@ -17,7 +17,7 @@ export default class PackAPI {
public static downloadSinglePack = async (
req: Request,
res: Response,
res: any,
next: NextFunction
) => {
if (!req.query.name) {
@ -33,11 +33,6 @@ export default class PackAPI {
});
}
const zipFile = zip(foundPack.files);
response.pipe(zipFile);
return res.status(200).json({
message: "ok",
});
return res.zip(foundPack.files);
};
}

29
src/pack_upload.ts Normal file
View File

@ -0,0 +1,29 @@
import { Request, Response, NextFunction, response } from "express";
import os from "os";
const uploadsFolder = `${os.homedir()}/beatloads`;
export default class PackUploader {
public static uploadPack = async (
req: any,
res: Response,
next: NextFunction
) => {
let packName;
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send("No files were uploaded.");
}
try {
const packFile = req.files.pack;
packName = req.files.pack.name;
const path = `${uploadsFolder}/${packName}`;
await packFile.mv(path);
} catch (error) {
return res.status(500).send(error);
}
return res.status(200).send("File uploaded!");
};
}

8
src/routes/global.ts Normal file
View File

@ -0,0 +1,8 @@
import express from "express";
import PackAPI from "../pack_api";
import PackUploader from "../pack_upload";
import GlobalService from "../global_service";
export const globalRoutes = express.Router();
globalRoutes.get("/versions", GlobalService.getVersions);

View File

@ -1,7 +1,9 @@
import express from "express";
import PackAPI from "../pack_api";
import PackUploader from "../pack_upload";
export const packRoutes = express.Router();
packRoutes.get("/packs", PackAPI.getPacks);
packRoutes.get("/getpack", PackAPI.downloadSinglePack);
packRoutes.post("/upload", PackUploader.uploadPack);

View File

@ -1,11 +1,14 @@
import http from "http";
import express, { Express } from "express";
import morgan from "morgan";
import os from "os";
import { packRoutes } from "./routes/packs";
import { globalRoutes } from "./routes/global";
import PackAPI from "./pack_api";
import PackService from "./services/pack_service";
const router: Express = express();
const fileUpload = require("express-fileupload");
/** Logging */
router.use(morgan("dev"));
@ -14,6 +17,19 @@ router.use(express.urlencoded({ extended: false }));
/** Takes care of JSON data */
router.use(express.json());
/** File Upload */
router.use(
fileUpload({
useTempFiles: true,
tempFileDir: `${os.homedir()}/beatloads/tmp`,
createParentPath: true,
uriDecodeFileNames: true,
safeFileNames: true,
preserveExtension: true,
debug: true,
})
);
/** RULES OF OUR API */
router.use((req, res, next) => {
// set the CORS policy
@ -33,7 +49,7 @@ router.use((req, res, next) => {
/** Routes */
router.use("/", packRoutes);
router.use("/", globalRoutes);
/** Error handling */
router.use((req, res, next) => {
const error = new Error("not found");

View File

@ -2,10 +2,10 @@ import Pack from "../interfaces/pack";
import File from "../interfaces/file";
import fs from "fs";
import * as fsWalk from "@nodelib/fs.walk";
import os from 'os';
import os from "os";
import { resolve } from "path/posix";
const PACK_BASE_PATH =`${os.homedir()}/beatpacks`;
const PACK_BASE_PATH = `${os.homedir()}/beatpacks`;
export default class PackService {
private static packList: Pack[] = [];
@ -15,7 +15,7 @@ export default class PackService {
public static getInitialList = async (): Promise<void> => {
let packs = await fs.readdirSync(PACK_BASE_PATH, { withFileTypes: true });
packs = packs.filter((pack) => pack.isDirectory());
PackService.packList = await Promise.all(
packs.map((pack) => PackService.getPackInfo(pack.name))
);
@ -24,8 +24,8 @@ export default class PackService {
public static getPackInfo = async (packName: string): Promise<Pack> => {
let files: File[] = [];
const name = packName;
let size = 0;
let bpm: string = "";
let levels = 0;
// Setup walk
const noDots = (entry: fsWalk.Entry) => !entry.name.startsWith(".");
@ -37,16 +37,17 @@ export default class PackService {
const promises = await Promise.all(
packFiles.map(async (file) => {
size += await fs.statSync(file.path).size;
files.push({
path: file.path,
name: file.name
name: file.name,
});
if (file.name === "bpm.txt") {
bpm = await fs.readFileSync(file.path).toString();
}
if (file.name.includes('music')) {
levels++;
}
})
);
@ -56,9 +57,9 @@ export default class PackService {
const currentPack: Pack = {
name: name,
size: size,
bpm: bpm,
files: files,
levels: levels,
};
return currentPack;

View File

@ -82,6 +82,13 @@
"@types/connect" "*"
"@types/node" "*"
"@types/busboy@^0":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@types/busboy/-/busboy-0.3.2.tgz#2f29b017513415399c42632ae6a7cfcb1409b79c"
integrity sha512-iEvdm9Z9KdSs/ozuh1Z7ZsXrOl8F4M/CLMXPZHr3QuJ4d6Bjn+HBMC5EMKpwpAo8oi8iK9GZfFoHaIMrrZgwVw==
dependencies:
"@types/node" "*"
"@types/connect@*":
version "3.4.35"
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.35.tgz#5fcf6ae445e4021d1fc2219a4873cc73a3bb2ad1"
@ -89,6 +96,14 @@
dependencies:
"@types/node" "*"
"@types/express-fileupload@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/express-fileupload/-/express-fileupload-1.2.2.tgz#98c10e900c222744bba16c848505a1fa95ab3ff0"
integrity sha512-sWU1EVFfLsdAginKVrkwTRbRPnbn7dawxEFEBgaRDcpNFCUuksZtASaAKEhqwEIg6fSdeTyI6dIUGl3thhrypg==
dependencies:
"@types/busboy" "^0"
"@types/express" "*"
"@types/express-serve-static-core@^4.17.18":
version "4.17.28"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.28.tgz#c47def9f34ec81dc6328d0b1b5303d1ec98d86b8"
@ -98,7 +113,7 @@
"@types/qs" "*"
"@types/range-parser" "*"
"@types/express@^4.17.13":
"@types/express@*", "@types/express@^4.17.13":
version "4.17.13"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.13.tgz#a76e2995728999bab51a33fabce1d705a3709034"
integrity sha512-6bSZTPaTIACxn48l50SR+axgrqm6qXFIxrdAKaG6PaJk3+zuUr35hBlgT7vOmJcum+OEaIBLtHV/qloEAFITeA==
@ -318,6 +333,13 @@ buffer@^5.1.0:
base64-js "^1.3.1"
ieee754 "^1.1.13"
busboy@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/busboy/-/busboy-0.3.1.tgz#170899274c5bf38aae27d5c62b71268cd585fd1b"
integrity sha512-y7tTxhGKXcyBxRKAni+awqx8uqaJKrSFSNFSeRG5CsWNdmy2BIK+6VGWEW7TZnIO/533mtMEA4rOevQV815YJw==
dependencies:
dicer "0.3.0"
bytes@3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5"
@ -518,6 +540,13 @@ destroy@~1.0.4:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=
dicer@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/dicer/-/dicer-0.3.0.tgz#eacd98b3bfbf92e8ab5c2fdb71aaac44bb06b872"
integrity sha512-MdceRRWqltEG2dZqO769g27N/3PXfcKl04VhYnBlo2YhH7zPi88VebsjTKclaOyiuMaGU72hTfw3VkUitGcVCA==
dependencies:
streamsearch "0.1.2"
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
@ -572,6 +601,13 @@ etag@~1.8.1:
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
express-fileupload@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/express-fileupload/-/express-fileupload-1.3.1.tgz#3238472def305b8cb4cc5936a953761d0c442011"
integrity sha512-LD1yabD3exmWIFujKGDnT1rmxSomaqQSlUvzIsrA1ZgwCJ6ci7lg2YHFGM3Q6DfK+Yk0gAVU7GWLE7qDMwZLkw==
dependencies:
busboy "^0.3.1"
express-zip@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/express-zip/-/express-zip-3.0.0.tgz#f2590d5dd895842b51e9593c2af8dbfe3e5190f0"
@ -1355,6 +1391,11 @@ signal-exit@^3.0.2:
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=
streamsearch@0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-0.1.2.tgz#808b9d0e56fc273d809ba57338e929919a1a9f1a"
integrity sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=
string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.2:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"