-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
119 lines (108 loc) · 2.82 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
// server.js
// where your node app starts
// init project
require('dotenv').config()
const fs = require("fs");
const url = require("url");
const path = require("path");
const express = require("express");
const app = express();
const RSS = require("rss");
app.use(express.static("public"));
app.set("json spaces", " ");
// Return an array of asset objects for files that haven't been deleted
const parseGlitchAssets = async () => {
const handle = await fs.promises.open(".glitch-assets");
const contents = await handle.readFile({ encoding: "utf8" });
const lines = contents.split("\n").filter(line => line.length);
const records = lines.map(line => {
try {
return JSON.parse(line);
} catch (err) {
console.error(err);
return {};
}
});
const assets = new Map();
// Iterate through the records to obtain the current set of assets
for (const record of records) {
if (record.deleted) {
assets.delete(record.uuid);
} else {
assets.set(record.uuid, record);
}
}
await handle.close();
return assets;
};
// Make a feed item out of an asset.
const assetToFeedItem = asset => {
const title = path.parse(asset.name).name
return {
title: title,
description: `A file named ${title}`,
url: asset.url,
guid: asset.uuid,
date: asset.date,
enclosure: {
url: asset.url,
size: asset.size,
type: asset.type
},
custom_elements: [
{
"itunes:image": {
_attr: {
href: asset.thumbnail
}
}
}
]
};
};
// Show the items in JSON format
app.get("/", async function(request, response) {
parseGlitchAssets()
.then(assets => {
const responseArray = [...assets.values()].map(assetToFeedItem)
response.json(responseArray)
})
.catch(err => {
console.error(err);
response.status(500).send(err);
});
});
const makeURL = (req, pathname = req.path) => {
return url.format({
protocol: req.protocol,
hostname: req.hostname,
pathname: pathname
});
};
const makeFeed = async request => {
const feed = new RSS({
title: "Noelle's Goodies",
feed_url: makeURL(request),
site_url: makeURL(request, ""),
custom_namespaces: {
itunes: "http://www.itunes.com/dtds/podcast-1.0.dtd"
}
});
const assets = await parseGlitchAssets();
for (const asset of assets.values()) {
feed.item(assetToFeedItem(asset));
}
return feed;
};
app.get("/feed", async function(request, response) {
makeFeed(request)
.then(feed => response.type("application/rss+xml").send(feed.xml(" ")))
.catch(err => {
console.error(err);
response.status(500).send(err);
});
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});