Compare commits

..

No commits in common. "ccee09189a0d9e1d4b9e882da9f494ceedb3394d" and "27d4d3508e6dfb5448bc31cae42fd6b0ccc5d930" have entirely different histories.

11 changed files with 26 additions and 135 deletions

View File

@ -7,16 +7,13 @@
"license": "MIT",
"scripts": {
"dev": "nodemon src/server.ts",
"lint": "prettier --write src",
"build": "rm -rf build/ && tsc"
"build": "rm -rf build/ && prettier --write src && 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",

View File

@ -1,14 +0,0 @@
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,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: any,
res: Response,
next: NextFunction
) => {
if (!req.query.name) {
@ -33,6 +33,11 @@ export default class PackAPI {
});
}
return res.zip(foundPack.files);
const zipFile = zip(foundPack.files);
response.pipe(zipFile);
return res.status(200).json({
message: "ok",
});
};
}

View File

@ -1,29 +0,0 @@
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!");
};
}

View File

@ -1,8 +0,0 @@
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,9 +1,7 @@
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,14 +1,11 @@
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"));
@ -17,19 +14,6 @@ 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
@ -49,7 +33,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,7 +2,7 @@ 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`;
@ -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,17 +37,16 @@ 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++;
}
})
);
@ -57,9 +56,9 @@ export default class PackService {
const currentPack: Pack = {
name: name,
size: size,
bpm: bpm,
files: files,
levels: levels,
};
return currentPack;

View File

@ -82,13 +82,6 @@
"@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"
@ -96,14 +89,6 @@
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"
@ -113,7 +98,7 @@
"@types/qs" "*"
"@types/range-parser" "*"
"@types/express@*", "@types/express@^4.17.13":
"@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==
@ -333,13 +318,6 @@ 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"
@ -540,13 +518,6 @@ 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"
@ -601,13 +572,6 @@ 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"
@ -1391,11 +1355,6 @@ 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"