-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileuplaod.js
116 lines (93 loc) · 3.12 KB
/
fileuplaod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var fs = Npm.require('fs'),
path = Npm.require('path'),
basePath = path.resolve('.').split('.meteor')[0];
var Promise = require('bluebird'),
GoogleCloudStorage = Promise.promisifyAll(require('@google-cloud/storage')),
storage = GoogleCloudStorage({
projectId: 'beaming-courage-166808',
keyFilename: basePath + 'private/my-gcs-key-public.json'
}),
BUCKET_NAME = 'react-meteor-bucket',
myBucket = storage.bucket(BUCKET_NAME)
Meteor.methods({
deleteFile: function (path) {
path = getFileName();
var file = myBucket.file(path);
file.existsAsync(function (err, exists) {
if (exists) {
file.delete(function (err) {
if (err) {
console.log(err);
} else {
return true;
}
});
} else {
console.log("No exists")
return false;
}
});
function getFileName() {
var filename = path.split("/");
return (filename[filename.length - 1])
}
},
uploadtogcs: function (fileInfo, fileData, encoding) {
var filename = rename(cleanName(fileInfo.name)) || 'file',
encoding = encoding || 'binary';
var file = myBucket.file(filename);
file.existsAsync(function (err, exists) {
if (exists) {
filename = rename(filename);
}
fs.writeFile(filename, fileData, encoding, function (err) {
if (err) {
return err;
}
myBucket.uploadAsync(filename, { public: true }).then(file => {
fs.unlinkSync(filename);
console.log("uploaded file")
});
});
});
return { "publicurl": publicUrl(filename), "filename": fileInfo.name, "size": fileInfo.size, "type": fileInfo.type };
function publicUrl(filename) {
return "https://storage.googleapis.com/react-meteor-bucket/" + filename;
}
function rename(filename) {
var filename = filename.split(".");
return filename[0] + "(" + Date.now() + ")" + "." + filename[filename.length - 1]
}
function cleanPath(str) {
if (str) {
return str.replace(/\.\./g, '').replace(/\/+/g, '').replace(/^\/+/, '').replace(/\/+$/, '');
}
};
function cleanName(str) {
return str.replace(/\.\./g, '').replace(/\//g, '');
};
}
});
Slingshot.createDirective("myFileUploads", Slingshot.GoogleCloud, {
GoogleAccessId: "my-sevice@beaming-courage-166808.iam.gserviceaccount.com",
GoogleSecretKey: Assets.getText('google-cloud-service-key-react.pem'),
bucket: "react-meteor-bucket",
maxSize: 20 * 1024 * 1024,
allowedFileTypes: ['image/png', "image/gif", "image/jpeg", "text/plain"],
// Uploaded files are publicly readable:
acl: "public-read",
// Deny uploads if user is not logged in:
authorize: function () {
if (!this.userId) {
var message = "Please login before posting files";
throw new Meteor.Error("Login Required", message);
}
return true;
},
// Store files into a directory by the current users username:
key: function (file) {
//Store file into a directory by the user's username.
var user = Meteor.users.findOne(this.userId);
return user.username + "/" + file.name;
}
});