This repository was archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.js
185 lines (163 loc) · 4.77 KB
/
queries.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const { format } = require('util')
const multer = require('multer')
const path = require('path')
const fs = require('fs')
const { Storage } = require('@google-cloud/storage')
const serviceKey = path.join(__dirname, './config/keys.json');
const serviceKeyCut = require('./config/keyscut.json');
const serviceKeyJoined = { ...serviceKeyCut, "private_key_id": process.env.private_key_id, "private_key": process.env.private_key.replace(/\\n/gm, '\n')};
fs.writeFileSync(serviceKey, JSON.stringify(serviceKeyJoined));
const Pool = require('pg').Pool;
const pool = new Pool({
user: process.env.user,
host: process.env.host,
database: process.env.database,
password: process.env.dbpass,
port: 5432,
})
const GOOGLE_CLOUD_PROJECT = "my-unsplash-366500";
const GCLOUD_STORAGE_BUCKET = "my-unsplash-store";
const gStorage = new Storage({
keyFilename: serviceKey,
projectId: GOOGLE_CLOUD_PROJECT
});
const bucket = gStorage.bucket(GCLOUD_STORAGE_BUCKET);
const getImages = (req, res) => {
pool.query('SELECT id, tag, url FROM images3 ORDER BY id ASC', (err, results) => {
if (err) {
throw err
}
res.status(200).json(results.rows)
})
}
const getImageById = (req, res) => {
const id = parseInt(req.params.id);
pool.query('SELECT * FROM images3 WHERE id = $1', [id], (err, results) => {
if (err) {
throw err
}
res.status(200).json(results.rows)
})
}
const addImage = (req, res) => {
const { tag, url } = req.body.data;
const password = Math.random().toString(36).substring(2, 7);
pool.query(
'INSERT INTO images3 (tag, url, password, gcp) VALUES ($1, $2, $3, false) RETURNING *',
[tag, url, password], (err, results) => {
if (err) {
throw err
}
// res.status(201).send(`Image added with ID: ${results.rows[0].id}`)
res.status(201).json(results.rows)
})
}
const addImageFile = (req, res, next) => {
const { tag } = req.body;
const password = Math.random().toString(36).substring(2, 7);
if (!req.file) {
res.status(400).send('No file uploaded');
return
}
const blob = bucket.file(req.file.originalname);
const blobStream = blob.createWriteStream();
blobStream.on('error', err => {
next(err)
});
blobStream.on('finish', () => {
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
);
const url = publicUrl;
pool.query(
'INSERT INTO images3 (tag, url, password, gcp) VALUES ($1, $2, $3, true) RETURNING *',
[tag, url, password], (err, results) => {
if (err) {
throw err
}
// res.status(201).send(`Image added with ID: ${results.rows[0].id} and url: ${url}`)
res.status(201).json(results.rows)
})
});
blobStream.end(req.file.buffer)
}
const updateImage = (req, res) => {
const id = parseInt(req.params.id);
const { tag, url } = req.body.data;
pool.query(
'UPDATE images3 SET tag = $1, url = $2 WHERE id = $3',
[tag, url, id], (err, results) => {
if (err) {
throw err
}
res.status(200).send(`Image modified with ID: ${id}`)
}
)
}
const deleteImage = (req, res) => {
const id = parseInt(req.params.id);
const { password } = req.body;
pool.query('SELECT * FROM images3 WHERE id = $1 AND password = $2',
[id, password], (err, results) => {
if (err) {
throw err
}
if (!results.rows[0]) {
res.status(403).send('Image id / password mismatch')
} else {
if (results.rows[0].gcp) {
const file = bucket.file(results.rows[0].url.substr(49));
file.delete((err, apiResponse) => {
if (err) {
throw err
}
console.log('204 means the google bucket delete was successful -->', apiResponse.statusCode);
})
};
pool.query('DELETE FROM images3 WHERE id = $1',
[id], (err, results) => {
if (err) {
throw err
}
res.status(200).send(`Image deleted with ID: ${id}`)
}
)
}
})
}
const deleteImageAdmin = (req, res) => {
const id = parseInt(req.params.id);
pool.query('DELETE FROM images3 WHERE id = $1',
[id], (err, results) => {
if (err) {
throw err
}
res.status(200).send(`Image deleted with ID: ${id}`)
})
}
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 10000000,
},
fileFilter(res, file, cb) {
if (
!file.originalname.endsWith('.jpg') &&
!file.originalname.endsWith('.jpeg') &&
!file.originalname.endsWith('.png')
) {
return cb(new Error('Please upload a valid image!'));
}
cb(undefined, true);
},
});
module.exports = {
getImages,
getImageById,
addImage,
addImageFile,
updateImage,
deleteImage,
deleteImageAdmin,
upload,
}