Skip to content

Commit 9dfad52

Browse files
committedMay 28, 2021
Merge branch 'main' of https://github.com/dropbucket/dropbucket-backend into kty
# Conflicts: # package-lock.json # package.json
2 parents 207875b + 9a4b552 commit 9dfad52

File tree

5 files changed

+69
-21
lines changed

5 files changed

+69
-21
lines changed
 

‎package-lock.json

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"debug": "~2.6.9",
1414
"dotenv": "^9.0.2",
1515
"express": "~4.16.1",
16-
"express-jwt": "^6.0.0",
1716
"jwt-decode": "^3.1.2",
1817
"morgan": "~1.9.1",
1918
"multer": "^1.4.2",

‎src/controllers/favoritesController.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55

66
export const showFavorites = async (req, res, next) => {
77
try {
8-
let rows = await showFavorites2(req.body);
8+
let rows = await showFavorites2(req);
99
return res.json(rows);
1010
} catch (err) {
1111
return res.status(500).json(err);
@@ -14,7 +14,7 @@ export const showFavorites = async (req, res, next) => {
1414

1515
export const switchFavorites = async (req, res, next) => {
1616
try {
17-
let rows = await switchFavorites2(req.body);
17+
let rows = await switchFavorites2(req);
1818
return res.json(rows);
1919
} catch (err) {
2020
return res.status(500).json(err);

‎src/services/favoritesService.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
import AWS from 'aws-sdk';
22
import dotenv from 'dotenv';
3+
import jwt_decode from 'jwt-decode';
34
dotenv.config();
45

6+
const me = async function (req) {
7+
if (req.headers && req.headers.authorization) {
8+
let authorization = req.headers.authorization.split(' ')[1],
9+
decoded;
10+
try {
11+
decoded = jwt_decode(authorization);
12+
} catch (e) {
13+
return { statusCode: 401, msg: 'unauthorized' };
14+
}
15+
let userId = decoded.user_id;
16+
// Fetch the user by id
17+
return { statusCode: 200, userId: userId };
18+
}
19+
return { statusCode: 500, msg: 'Header error' };
20+
};
21+
522
export const showFavorites2 = async (req) => {
623
console.log('connect');
24+
725
try {
826
AWS.config.update({
927
region: process.env.AWS_REGION,
@@ -13,13 +31,19 @@ export const showFavorites2 = async (req) => {
1331
//sessionToken: process.env.DYNAMODB_SESSION_TOKEN,
1432
});
1533

34+
const im = await me(req);
35+
if (im.statusCode === 401 || im.statusCode === 500) {
36+
return im;
37+
}
38+
const file_owner = im.userId;
39+
1640
let docClient = new AWS.DynamoDB.DocumentClient();
1741
let params = {
1842
TableName: 'FileDirTable',
1943
FilterExpression: 'is_starred = :tr and file_owner = :fo',
2044
ExpressionAttributeValues: {
2145
':tr': true,
22-
':fo': req.file_owner,
46+
':fo': file_owner,
2347
},
2448
};
2549

@@ -51,12 +75,18 @@ export const switchFavorites2 = async (req) => {
5175
//sessionToken: process.env.DYNAMODB_SESSION_TOKEN,
5276
});
5377

78+
const im = await me(req);
79+
if (im.statusCode === 401 || im.statusCode === 500) {
80+
return im;
81+
}
82+
const file_owner = im.userId;
83+
5484
let docClient = new AWS.DynamoDB.DocumentClient();
5585
let params = {
5686
TableName: 'FileDirTable',
5787
Key: {
58-
parent_id: req.parent_id,
59-
id: req.id,
88+
file_owner: file_owner,
89+
id: req.body.id,
6090
},
6191
};
6292

@@ -67,8 +97,8 @@ export const switchFavorites2 = async (req) => {
6797
params = {
6898
TableName: 'FileDirTable',
6999
Key: {
70-
parent_id: req.parent_id,
71-
id: req.id,
100+
file_owner: file_owner,
101+
id: req.body.id,
72102
},
73103
UpdateExpression: 'set is_starred = :n',
74104
ExpressionAttributeValues: {

‎src/services/shareService.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,46 @@ export const shareItem2 = async (req) => {
1515
let docClient = new AWS.DynamoDB.DocumentClient();
1616
let params = {
1717
TableName: 'FileDirTable',
18-
Key: {
19-
parent_id: req.parent_id,
20-
id: req.id,
21-
},
22-
UpdateExpression: 'set is_shared = :n',
18+
FilterExpression: 'id = :id',
2319
ExpressionAttributeValues: {
24-
':n': true,
20+
':id': req.id,
2521
},
26-
ReturnValues: 'UPDATED_NEW',
2722
};
2823

29-
let status = await docClient.update(params).promise();
30-
console.log(JSON.stringify(status, null, 2));
24+
let fileArr = await docClient.scan(params).promise();
3125

26+
if (fileArr.Count === 0) {
27+
return {
28+
statusCode: 500,
29+
success: false,
30+
msg: '해당하는 id의 파일이 존재하지않습니다.',
31+
};
32+
}
33+
let file = fileArr.Items[0];
3234
params = {
3335
TableName: 'FileDirTable',
3436
Key: {
35-
parent_id: req.parent_id,
37+
file_owner: file.file_owner,
3638
id: req.id,
3739
},
40+
UpdateExpression: 'set is_shared = :n',
41+
ExpressionAttributeValues: {
42+
':n': true,
43+
},
44+
ReturnValues: 'UPDATED_NEW',
3845
};
3946

40-
let data = await docClient.get(params).promise();
47+
let status = await docClient.update(params).promise();
48+
console.log(file);
49+
4150
let s3 = new AWS.S3();
42-
//console.log(data.Item.filename);
4351

4452
// key값 수정 필요 ! s3에 파일이 어떻게 저장 돼있는지에 따라 수정
4553
const BUCKET_NAME = 'dropbucket123';
4654
params = {
4755
Bucket: BUCKET_NAME,
48-
Key: data.Item.filename + '.' + data.Item.content_type,
56+
Key: file.id,
57+
//Key: file.id + '.' + file.content_type,
4958
};
5059

5160
let url = await s3.getSignedUrlPromise('getObject', params);

0 commit comments

Comments
 (0)
Please sign in to comment.