Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
release v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Aug 6, 2023
1 parent 42fe774 commit 229d1b6
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 38 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@ jobs:
- name: Build Package
run: |
cd server
echo '{}' > test.json
zip res.zip test.json
npx pkg . -t node18-x64-windows
mkdir -p out/web
cp -r web/* out/web/
mkdir -p out/saves
cp config.json out/
cp -r web/* out/web/
cp -r res.zip out/saves/
cd ..
- uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maa-json-viewer",
"version": "1.0.0",
"version": "1.0.1",
"private": true,
"scripts": {
"dev": "vite",
Expand Down
7 changes: 7 additions & 0 deletions server/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"port": 8080,
"web": "./web",
"date": "yyyyMMdd-HHmmss",
"saves": "./saves/{date}.zip",
"active": "./saves/res.zip"
}
35 changes: 35 additions & 0 deletions server/package-lock.json

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

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@types/express": "^4.17.17",
"@types/multer": "^1.4.7",
"@types/node": "^18.17.2",
"date-fns": "^2.30.0",
"esbuild": "^0.18.17",
"express": "^4.18.2",
"multer": "^1.4.5-lts.1",
Expand Down
83 changes: 48 additions & 35 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,56 @@ import express, { json } from 'express'
import multer, { memoryStorage } from 'multer'
import path from 'path'
import fs from 'fs/promises'
import { format } from 'date-fns'

async function main() {
const config = JSON.parse(await fs.readFile('config.json', 'utf-8')) as {
port: number
web: string
date: string
saves: string
active: string
}

const app = express()

const webDir = path.resolve('./web')
const port = 8080

app.use(express.static(webDir))
app.use(json())

let activeZip = './saves/res.zip'

app.post('/api/zip', async (req, res) => {
res.sendFile(path.resolve(activeZip))
})

const cache = memoryStorage()
const multerInst = multer({
storage: cache
})

app.post('/api/save', multerInst.single('file'), async (req, res) => {
const buffer = req.file?.buffer
if (buffer) {
const newFileName = `./saves/${Date.now()}.zip`
fs.writeFile(newFileName, buffer).then(() => {
activeZip = newFileName
const app = express()

app.use(express.static(config.web))
app.use(json())

app.post('/api/load', async (req, res) => {
res.sendFile(path.resolve(config.active))
})

const cache = memoryStorage()
const multerInst = multer({
storage: cache
})

app.post('/api/save', multerInst.single('file'), async (req, res) => {
const buffer = req.file?.buffer
if (buffer) {
const newFileName = config.saves.replaceAll(
'{date}',
format(new Date(), config.date)
)
await fs.mkdir(path.dirname(newFileName), { recursive: true })
await fs.writeFile(newFileName, buffer)
console.log('saved to', newFileName)
config.active = newFileName
fs.writeFile('config.json', JSON.stringify(config, null, 2))
res.send({
success: true
})
})
} else {
res.send({
success: false
})
}
})
} else {
res.send({
success: false
})
}
})

app.listen(config.port, () => {
console.log(`server started: http://localhost:${config.port}/`)
})
}

app.listen(port, () => {
console.log(`server started: http://localhost:${port}/`)
})
main()
2 changes: 1 addition & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fs } from './data/fs'

async function loadZip() {
const res: ArrayBuffer = (
await axios.post('/api/zip', null, {
await axios.post('/api/load', null, {
responseType: 'arraybuffer'
})
).data
Expand Down

0 comments on commit 229d1b6

Please sign in to comment.