-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
185 lines (160 loc) · 4.73 KB
/
server.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
require('dotenv').config();
const s3Zip = require('s3-zip');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(express.json());
const axios = require('axios');
const getAllTags = require('./Get-All-Tags/getAllTags'); // Import getAllTags function
// CORS: Allow requests from these origins:
const allowedOrigins = [
'http://127.0.0.1:5173',
'http://localhost:5173/',
'https://meeting-background-maker.surge.sh'
];
const corsOptions = {
origin: function (origin, callback) {
/*
Check if the origin is in the allowedOrigins array
or if it's undefined (e.g., same-origin requests)
*/
if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
};
app.use(cors(corsOptions));
// set the port, either from an environmental variable or manually
const port = process.env.PORT || 3001;
/*
**********************************
index route
**********************************
*/
app.get('/', (req, res, next) => {
res.send(`Hello world, let's make some meeting backgrounds.`);
});
/*
**********************************
Route to get all tags
(to create the dropdown menu)
**** NOT USING AT THE MOMENT. ****
We used this once to get the values we needed.
The values do not change, so there is no need
to keep hitting the API.
We're keeping the function in case we do want
to retrieve the values again.
**********************************
*/
app.get('/alltags/', async (req, res) => {
try {
const tags = await getAllTags(); // Use getAllTags function
res.json(tags);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});
/*
**********************************
Gets all the items a value that matches keyword
**********************************
*/
app.get('/searchbytag/:value', cors(), (req, res, error) => {
const { value } = req.params;
const url = `https://api.collection.cooperhewitt.org/rest/?method=cooperhewitt.search.objects&access_token=${process.env.COOPER_API_TOKEN}&has_images=1&per_page=20&tag=${value}`;
axios({
url: url,
method: 'get',
// DANGER: only use for development or debugging
// For development or debugging, you can ignore SSL verification
httpsAgent: new (require('https').Agent)({
rejectUnauthorized: false,
}),
})
.then((response) => {
let data = response.data.objects;
return res.json(data);
})
.catch(function (error) {
console.log('searchbytag error:', error);
});
});
const { GetObjectCommand, S3, S3Client } = require('@aws-sdk/client-s3');
const { Readable } = require('stream');
const streamToString = require('stream-to-string');
/* **********************************
zip selected files in aws
note: using npm package s3-zip
https://github.com/orangewise/s3-zip
When the use hits the "Download Collection as Zip File"
button, an axios call is send from
********************************** */
app.get('/download', (req, res) => {
const awsBucketName = process.env.AWS_BUCKET_NAME;
const region = process.env.AWS_BUCKET_REGION;
const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
const secretAccessKey = process.env.AWS_SECRET_KEY;
const s3Bucket = new S3({
region,
credentials: {
accessKeyId,
secretAccessKey,
},
});
console.log('🗜🗜🗜🗜 s3zip req.query:', req.query);
/*
The list of image jpegs comes from the client
as an object called req.query.
We use Object.values() to put the values into
an array called jpegFiles, which we pass into
s3Zip
*/
const jpegFiles = Object.values(req.query);
const folder = 'meeting-backgrounds/';
s3Zip
.archive(
{
s3: s3Bucket,
region: region,
bucket: awsBucketName,
preserveFolderStructure: true,
},
folder,
jpegFiles
)
.pipe(res.attachment());
});
/*
**********************************
Error Handlers
**********************************
*/
// Error handling middleware
app.use((err, req, res, next) => {
// Log the error for debugging purposes
console.error(err);
// Send an appropriate response to the client
res.status(500).send('500 Internal Server Error');
// If you don't use `next`, you can omit it, but it's good to include for consistency
next();
});
app.use((req, res, next) => {
res.sendStatus(404)
// If you don't use `next`, you can omit it, but it's good to include for consistency
next();
});
/*
**********************************
Port
**********************************
*/
app
.listen(port, () => {
console.log(
`Let's get some meeting backgrounds! Listening on port: ${port}, in ${app.get('env')} mode.`
);
})
.on('port error:', console.error);
module.exports = app;