-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (157 loc) · 6.14 KB
/
index.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
import 'dotenv/config'
//console.log(process.env);
import express from "express";
import fetch from "node-fetch";
import proxy from 'express-http-proxy';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { Post } from "./helpers/post.js";
import { colors } from './helpers/log.js';
import { order, rating } from './helpers/regex.js';
import { setFields } from './helpers/fields.js';
let baseURL = process.env.BASEURL_SZURUBOORU || "http://szurubooru.int";
let redirectURL = process.env.REDIRECTURL_INTERNAL_STACK || "http://172.20.1.3";
let debug = !!(+process.env.DEBUG || false);
redirectURL = redirectURL == `client` ? `http://${redirectURL}` : redirectURL
const app = express();
app.use((req, res, next) => {
debug ? console.log(`[${colors.FgCyan}Debug${colors.Reset}] ${req.socket.remoteAddress} request ${req.url}`) : "";
next();
})
app.use('/api/uploads', createProxyMiddleware({
target: `${redirectURL}`,
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
logger: console,
}));
if (debug == true) {
/*app.use('/api/posts*', proxy(`${redirectURL}`, {
proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
proxyReqOpts.headers['accept'] = 'application/json';
proxyReqOpts.headers['content-type'] = 'application/json';
proxyReqOpts.headers['cookie'] = srcReq.headers.cookie;
return proxyReqOpts;
}
}));*/
app.get('/api/posts*', async (req, res) => {
console.log(req.headers);
console.log (`${redirectURL}${req.url}`);
let fet = await fetch(`${redirectURL}${req.url}`, {
"headers": {
...req.headers
},
"body": null,
"method": "GET"
});
res.json(await fet.json());
})
}
app.get('/posts.json?*', async (req, res) => {
debug ? console.log(`[${colors.FgCyan}Debug${colors.Reset}] ${req.socket.remoteAddress} got request query ${req.query.toString()}`) : "";
let url = `${redirectURL}/api/posts?`
for (var k in req.query) {
if (k === "tags") {
let tags = req.query[k];
let tag = tags.split(" ");
for (let t in tag) {
tag[t] = decodeURIComponent(tag[t])
console.log(tag[t]);
if (tag[t].includes("rating")) {
tag[t] = rating(tag[t]);
}else if(tag[t].includes("order")){
tag[t] = order(tag[t]);
}
}
tags = tag.join(" ");
if(tags!=""){
url += `query=${tags}&`;
}
continue;
}
if ( k === "fields"){
continue;
}
url += `${k}=${req.query[k]}&`;
if (k === "limit") {
if (typeof req.query['page'] != "undefined") {
let offset = req.query['page'] * req.query['limit'] - req.query['limit'];
url += `offset=${offset}&`;
}
}
}
debug ? console.log(`[${colors.FgCyan}Debug${colors.Reset}] ${req.socket.remoteAddress} request SZURUBOORU api with url ${url}fields=${setFields("all")}`) : "";
let fet = await fetch(`${url}fields=${setFields("all")}`, {
"headers": {
"accept": "application/json",
"content-type": "application/json"
},
"body": null,
"method": "GET"
});
//debug ? console.dir(fet) : "";
let json = await fet.json();
//console.log(json);
let result = { posts: [] }
for (let post in json.results) {
//json.results[post]
//debug ? console.log(`[${colors.FgCyan}Debug${colors.Reset}] create Object of Post`) : "";
let p = new Post();
p.id = json.results[post].id;
// FILE
p.file = {
width: json.results[post].canvasWidth,
height: json.results[post].canvasHeight,
md5: json.results[post].checksumMD5,
url: `${baseURL}/${json.results[post].contentUrl}`,
ext: json.results[post].mimeType.split('/')[1]
}
//PREVIEW
p.preview = {
width: 150,
height: 84,
url: `${baseURL}/${json.results[post].thumbnailUrl}`
}
// Sample
p.sample = {
has: true,
alternates: {},
width: json.results[post].canvasWidth,
height: json.results[post].canvasHeight,
url: `${baseURL}/${json.results[post].contentUrl}`
}
let safety = {
"safe": "s",
"sketchy": "q",
"unsafe": "e"
}
p.rating = safety[json.results[post].safety] || "e"
//p.rating = json.results[post].safety == "safe" ? "" : "e"
p.created_at = json.results[post].created_at;
p.updated_at = json.results[post].lastEditTime == null ? json.results[post].created_at : json.results[post].lastEditTime;
let tag = {};
for (var l = 0; l < json.results[post].tags.length; l++) {
let oneTag = json.results[post].tags[l];
let name = oneTag.names;
let cat = oneTag.category;
if (typeof tag[cat] == "undefined") {
tag[cat] = [];
}
tag[cat].push(name[0]);
}
p.tags = tag;
//debug ? console.log(`[${colors.FgCyan}Debug${colors.Reset}] added Post ${p.id} to json`) : "";
result.posts.push(p);
}
debug ? console.log(`[${colors.FgCyan}Debug${colors.Reset}] ${req.socket.remoteAddress} got request query ${JSON.stringify(req.query)}`) : "";
debug ? console.log(`[${colors.FgCyan}Debug${colors.Reset}] ${req.socket.remoteAddress} send Posts.json to client`) : ""
res.send(result);
});
/*
Not working
sometimes entity to large (413) or method not allowed 405
app.use("/api/uploads",proxy(`${redirectURL}/api/uploads`,{
limit: 10 * 1024 * 1024 * 1024
}));
*/
app.use(proxy(redirectURL));
app.listen(5432,()=>{
console.log(`[${colors.FgCyan}Debug${colors.Reset}] online`)
});