diff --git a/.gitignore b/.gitignore index 8a579dc..11250a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules data *.code-workspace -.vscode \ No newline at end of file +.vscode +.env \ No newline at end of file diff --git a/build.docker.image.sh b/build.docker.image.sh new file mode 100644 index 0000000..e2caa9d --- /dev/null +++ b/build.docker.image.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# --- Configuration --- +IMAGE_NAME="172.22.102.100:40016/team-mmkb/simple-http-upload" +PACKAGE_JSON="package.json" + +# --- Functions --- + +# Function to get the current version from package.json +get_current_version() { + if [ ! -f "$PACKAGE_JSON" ]; then + echo "Error: $PACKAGE_JSON not found." + exit 1 + fi + jq -r '.version' "$PACKAGE_JSON" +} + +# Function to increment the major version +increment_major_version() { + local current_version="$1" + local major=$(echo "$current_version" | cut -d'.' -f1) + local minor=$(echo "$current_version" | cut -d'.' -f2) + local patch=$(echo "$current_version" | cut -d'.' -f3) + + minor=$((minor + 1)) + echo "$major.$minor.$patch" +} + +# Function to update the package.json with the new version +update_package_json() { + local new_version="$1" + jq ".version = \"$new_version\"" "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON" +} + +# --- Main Script --- + +# Get the current version +current_version=$(get_current_version) +echo "Current version: $current_version" + +# Increment the major version +new_version=$(increment_major_version "$current_version") +echo "New version: $new_version" + +# Update package.json +update_package_json "$new_version" +echo "Updated $PACKAGE_JSON with version: $new_version" + +# Build the Docker image with the new version +docker build -t "$IMAGE_NAME:$new_version" . +if [ $? -eq 0 ]; then + echo "Docker image built successfully: $IMAGE_NAME:$new_version" +else + echo "Error building docker image" + exit 1 +fi + +echo "Done!" diff --git a/index.js b/index.js index 325feb7..b761a7f 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +require('dotenv').config() const express = require('express'); const fileUpload = require('express-fileupload'); const fs = require('fs'); @@ -6,9 +7,11 @@ const app = express(); const port = 3000; const uploadPath = process.env.UPLOAD_PATH || '/tmp'; -// const config = process.env.CONFIG || "{ \"uploadGroups\": [] }"; +const config = process.env.CONFIG || "{ \"uploadGroups\": [] }"; //const config = process.env.CONFIG || "{ \"uploadGroups\": [{ \"matcher\": \"^mmkb-android-.*-test\\.apk$\", \"groupSize\": 3},{ \"matcher\": \"^mmkb-android-.*-int\\.apk$\", \"groupSize\": 3},{ \"matcher\": \"^mmkb-android-.*-prod\\.apk$\", \"groupSize\": 3}] }"; +console.info("Upload path: ", uploadPath); +console.info("Config: ", config); app.use(fileUpload()); @@ -48,14 +51,16 @@ const processGroups = () => { * clean up files based on the group size */ const cleanUp = () => { + console.info("Cleaning up files..."); const groups = processGroups(); + console.info("Groups: ", groups); groups.forEach(group => { if (group.files.length > group.groupSize) { const filesToDelete = group.files.slice(0, group.files.length - group.groupSize); filesToDelete.forEach(file => { console.log(new Date().toISOString() + " Deleting file: ", file); - fs.unlinkSync(uploadPath + "/" + file); + //fs.unlinkSync(uploadPath + "/" + file); }); } }); @@ -104,6 +109,13 @@ app.get('/state', (req, res) => { res.send(groups); }); +app.get('/clean', (req, res) => { + cleanUp(); + + res.setHeader('Content-Type', 'application/json'); + res.send('Clean up done!'); +}); + app.get('/', (req, res) => { const files = []; fs.readdirSync(uploadPath).forEach(file => { diff --git a/package-lock.json b/package-lock.json index f9981d0..2a762bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,15 @@ { "name": "simple-http-file-api", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "simple-http-file-api", - "version": "1.0.0", + "version": "1.1.0", "license": "ISC", "dependencies": { + "dotenv": "^16.4.7", "express": "^4.21.2", "express-fileupload": "^1.5.1", "mime": "^4.0.6" @@ -170,6 +171,18 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dotenv": { + "version": "16.4.7", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz", + "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/dunder-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", diff --git a/package.json b/package.json index 667f512..15b65ff 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,18 @@ { "name": "simple-http-file-api", - "version": "1.0.0", + "version": "1.1.0", "main": "index.js", "scripts": { "start": "node index.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "docker.build.image": "bash ./build.docker.image.sh", + "docker.push.image": "bash ./push.docker.image.sh" }, "author": "", "license": "ISC", "description": "", "dependencies": { + "dotenv": "^16.4.7", "express": "^4.21.2", "express-fileupload": "^1.5.1", "mime": "^4.0.6" diff --git a/push.docker.image.sh b/push.docker.image.sh new file mode 100644 index 0000000..72c1506 --- /dev/null +++ b/push.docker.image.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Get the current version from package.json +VERSION=$(jq -r '.version' package.json) + +# Build the Docker image with the updated version from package.json +docker push 172.22.102.100:40016/team-mmkb/simple-http-upload:$VERSION \ No newline at end of file