Skip to content

Commit

Permalink
[SSF] v1.1.1
Browse files Browse the repository at this point in the history
[Desc]
 - Change File Download Logic
 - Apply responsive width to upload dialog
  • Loading branch information
Kyusoo committed Mar 24, 2022
1 parent 07e510b commit c2cdaa9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-sharing-file",
"version": "1.1.0",
"version": "1.1.1",
"description": "Simple Sharing File",
"main": "dist/main.js",
"scripts": {
Expand Down
25 changes: 15 additions & 10 deletions src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,39 +156,44 @@ class Server {
console.log(`[POST] /auth [REQ] ${JSON.stringify(req.body)}`)

const password = req.body.password
let auth = false

if (!this.application.state.serverPassword || this.application.state.serverPassword.length === 0) {
res.json({ 'auth': true })
auth = true
res.json({ auth })
}
else {
if (this.application.state.serverPassword != password) {
res.json({ 'auth': false, 'message': 'Wrong Password' })

res.json({ auth, 'message': 'Wrong Password' })
}
else {
res.json({ 'auth': true, 'message': 'PASS' })
auth = true
res.json({ auth, 'message': 'PASS' })
this.sendFileList()
}
}

this.auth = auth
})
.post('/download', (req, res) => {
.get('/download/:fileName', (req, res) => {

console.log(`[POST] /download [REQ] ${JSON.stringify(req.body)}`)
const fileName = req.params.fileName
console.log(`[GET] /download [FileName] ${fileName}`)

const filename = req.body.filename
const file = path.resolve(this.application.state.sharePath, filename)
const file = path.resolve(this.application.state.sharePath, fileName)

try {
if (fs.existsSync(file)) {
res.status(200).download(file)
}
else {
res.status(500).json({ 'result': 'FAIL', 'message': `File doesn't exist` })
res.status(404).send('Not Found')
}
} catch (e) {
console.log(e)
console.log(e);
res.send(e.toString())
}

})
.post('/upload', upload.array('upload'), (req, res) => {

Expand Down
55 changes: 6 additions & 49 deletions src/server/views/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,55 +131,12 @@ const App = () => {
}

const downloadHandler = ({ target }) => {
const filename = target.firstChild.textContent
const url = `${location.origin}/download`
const fileName = target.firstChild.textContent
const url = `${location.origin}/download/${fileName}`

const xhr = new XMLHttpRequest()

console.log(`[Down] filename[${filename}] / url[${url}]`)

xhr.onload = e => {
try {
if (e.target.status === 200) {
const blob = e.target.response

let a = document.createElement("a");
a.style = "display: none";
document.body.appendChild(a);
//Create a DOMString representing the blob
//and point the link element towards it
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
//programatically click the link to trigger the download
a.click();
//release the reference to the file by revoking the Object URL
window.URL.revokeObjectURL(url);

document.body.removeChild(a)
}
else {
console.log(e.target.response)
const reader = new FileReader()
reader.onload = () => {
const result = JSON.parse(reader.result)
console.log(result)
setSnackBar({ ...snackbar, severity: 'error', open: true, message: result.message })
}
reader.readAsText(e.target.response)
}
} catch (error) {
console.log(error)
} finally {

}
}

const data = { filename }
xhr.responseType = 'blob'
xhr.open('POST', url)
xhr.setRequestHeader("Content-Type", "application/json")
xhr.send(JSON.stringify(data))
const link = document.createElement('a')
link.href = url
link.click()
}

const passwordChangeHandler = ({ target: { value } }) => {
Expand Down Expand Up @@ -362,7 +319,7 @@ const App = () => {
<Dialog open={dialog.open}>
<DialogTitle>{"File Upload"}</DialogTitle>
<DialogContent>
<Box sx={{ minWidth: 400 }}>
<Box sx={{ width: { xs: 200, sm: 200, md: 300, lg: 400, xl: 500 } }}>
<LinearProgressWithValue value={progress} />
</Box>
</DialogContent>
Expand Down

0 comments on commit c2cdaa9

Please sign in to comment.