Browse Source

Add config file and forbidden buckets to it

master
Alfred 2 years ago
parent
commit
a9a7aeed3c
4 changed files with 41 additions and 10 deletions
  1. 0
    3
      .gitignore
  2. 17
    0
      config/config.js
  3. 9
    1
      src/api/index.js
  4. 15
    6
      src/managers/s3manager.js

+ 0
- 3
.gitignore View File

@@ -73,6 +73,3 @@ package-lock.json
73 73
 
74 74
 # Data Bases
75 75
 **/*.sqlite
76
-
77
-# Config files
78
-config/*

+ 17
- 0
config/config.js View File

@@ -0,0 +1,17 @@
1
+module.exports = {
2
+    express: {
3
+        port: 8080,
4
+        bodyLimit: '100kb',
5
+        corsHeaders: ['Link']
6
+    },
7
+    s3: {
8
+        accessKeyId: "xxx",
9
+        secretAccessKey: "xxx",
10
+        region: "us-east-1",
11
+        endpoint: "http://s3.wasabisys.com",
12
+        url_template: "http://${bucket}.S3.wasabisys.com/${key}"
13
+    },
14
+    cloudviewer: {
15
+        forbidden_buckets: ["xx"]
16
+    }
17
+};

+ 9
- 1
src/api/index.js View File

@@ -8,7 +8,15 @@ export default ({ config, db }) => {
8 8
 
9 9
 	api.get('/', (req, res) => {res.json({ version });});
10 10
 	api.post('/', (req, res) => {
11
-		s3.list(req.body.bucket).then(function(data) {
11
+		let access_data = s3.access_data(req.body.bucket);
12
+		
13
+		if (config.get('cloudviewer.forbidden_buckets').includes(access_data.bucket)) {
14
+			res.status(400);
15
+			res.send('Bucket does not exist');
16
+			return;
17
+		}
18
+
19
+		s3.list(access_data).then(function(data) {
12 20
 			res.json(data);
13 21
 		}).catch(function(err) {
14 22
 			res.status(400);

+ 15
- 6
src/managers/s3manager.js View File

@@ -13,16 +13,25 @@ class S3Manager {
13 13
         this.url_template = config.get('s3.url_template')
14 14
     }
15 15
 
16
-    list(path) {
16
+    access_data(path) {
17
+        let bucket = path.split('/')[0];
18
+        let prefix = path.split('/').slice(1, 100).join('/');
19
+        prefix = (prefix.endsWith("/")) ? prefix : prefix + "/";
20
+        return {
21
+            bucket: bucket,
22
+            prefix: prefix
23
+        };
24
+    }
25
+
26
+    list(data) {
17 27
         let self = this;
18 28
         return new Promise(function(resolve, reject) {
19 29
             let last_data = null;
20 30
             let content = [];
21 31
     
22
-            let bucket = path.split('/')[0];
23
-            let prefix = path.split('/').slice(1, 100).join('/');
24
-            prefix = (prefix.endsWith("/")) ? prefix : prefix + "/";
25
-    
32
+            let bucket = data.bucket;
33
+            let prefix = data.prefix;
34
+
26 35
             let lo = self.client.listObjects({
27 36
                 s3Params: {
28 37
                     Bucket: bucket,
@@ -56,7 +65,7 @@ class S3Manager {
56 65
                 resolve({
57 66
                     content: content,
58 67
                     folders: folders,
59
-                    path: "/" + prefix,
68
+                    path: (prefix.length != 1) ? "/" + prefix : prefix,
60 69
                     bucket: bucket
61 70
                 });
62 71
             });

Loading…
Cancel
Save