Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit968a44e

Browse files
author
Filip Hric
committed
refactor image upload
1 parent88153b8 commit968a44e

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

‎backend/middleware.ts‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,39 @@ const socket = require('socket.io-client')('http://localhost:3001');
55

66
module.exports=(req,res,next)=>{
77

8-
const{ db}=req.app;
9-
8+
const{app:{db}, query}=req;
9+
1010
// prevent from caching database.json file
1111
// this is different from nocache(), which prevents 304 status codes
1212
db.assign(
1313
require('import-fresh')('./backend/data/database.json')
14-
).write();
15-
16-
if(req.method==='POST'&&req.path==='/upload'){
17-
constcardid=req.headers.cardid;
14+
).write();
15+
16+
if(req.method==='POST'&&req.path==='/upload'){
17+
constcardId=req.query.card;
1818

1919
letfstream;
2020
req.pipe(req.busboy);
2121
req.busboy.on('file',(fieldname,file,filename)=>{
2222
fstream=fs.createWriteStream(
23-
`${__dirname}/data/uploaded/${cardid}_${filename}`
23+
`${__dirname}/data/uploaded/${cardId}_${filename}`
2424
);
2525
file.pipe(fstream);
2626
fstream.on('close',()=>{
27+
28+
constcurrentCard=db
29+
.get('cards')
30+
.find({id:Number(query.card)})
31+
.value();
32+
33+
currentCard.image=`/data/uploaded/${cardId}_${filename}`
34+
35+
db.get('cards')
36+
.find({id:Number(query.card)}).write(currentCard)
37+
2738
res
28-
.status(201)
29-
.jsonp({path:`/data/uploaded/${cardid}_${filename}`});
39+
.status(200)
40+
.jsonp(currentCard);
3041
});
3142
});
3243

‎src/store/actions/uploadFile.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ export const uploadFile = async function (this: any, card: Card, acceptFile?: Fi
77
const{ id}=card;
88
file&&formData.append('image',file);
99
axios
10-
.post('/api/upload',formData,{
10+
.post(`/api/upload?card=${id}`,formData,{
1111
headers:{
12-
'Content-Type':'multipart/form-data',
13-
cardid:idasunknownasstring,
12+
'Content-Type':'multipart/form-data'
1413
},
1514
})
1615
.then((upload)=>{
1716
this.showNotification('File was sucessfully uploaded',false);
18-
constuploadedImagePath=upload.data.path;
19-
20-
this.patchCard(card,{image:uploadedImagePath});
17+
this.activeCard=upload.data;
2118
})
2219
.catch(()=>{
2320
this.showNotification('There was an error uploading file',true);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp