|
@@ -3,6 +3,8 @@ import axios from "axios";
|
|
|
import CoreClass from "../core";
|
|
|
import { MUSARE_VERSION } from "..";
|
|
|
|
|
|
+export const MUSICBRAINZ_CAA_CACHED_RESPONSE_REDIS_TABLE = "musicbrainzCAACachedResponse";
|
|
|
+
|
|
|
class RateLimitter {
|
|
|
/**
|
|
|
* Constructor
|
|
@@ -53,6 +55,7 @@ class _MusicBrainzModule extends CoreClass {
|
|
|
async initialize() {
|
|
|
DBModule = this.moduleManager.modules.db;
|
|
|
CacheModule = this.moduleManager.modules.cache;
|
|
|
+ AppModule = this.moduleManager.modules.app;
|
|
|
|
|
|
this.genericApiRequestModel = this.GenericApiRequestModel = await DBModule.runJob("GET_MODEL", {
|
|
|
modelName: "genericApiRequest"
|
|
@@ -62,6 +65,71 @@ class _MusicBrainzModule extends CoreClass {
|
|
|
this.requestTimeout = 5000;
|
|
|
|
|
|
this.axios = axios.create();
|
|
|
+
|
|
|
+ const { app } = await AppModule.runJob("GET_APP", {});
|
|
|
+
|
|
|
+ // Proxy with caching for coverartarchive.org
|
|
|
+ app.get(
|
|
|
+ [
|
|
|
+ "/caa/release/:mbid",
|
|
|
+ "/caa/release/:mbid/:type",
|
|
|
+ "/caa/release-group/:mbid",
|
|
|
+ "/caa/release-group/:mbid/:type"
|
|
|
+ ],
|
|
|
+ async (req, res) => {
|
|
|
+ // TODO add config option to proxy or redirect these requests
|
|
|
+
|
|
|
+ // Remove /caa/ from the path
|
|
|
+ const path = req.path.substring(5);
|
|
|
+ console.log(`Request for ${path}`);
|
|
|
+
|
|
|
+ const cachedResponse = await CacheModule.runJob("HGET", {
|
|
|
+ table: MUSICBRAINZ_CAA_CACHED_RESPONSE_REDIS_TABLE,
|
|
|
+ key: path
|
|
|
+ });
|
|
|
+ if (cachedResponse) {
|
|
|
+ const { contentType, data, status } = cachedResponse;
|
|
|
+ if (status === "404") {
|
|
|
+ console.log(contentType, data, status);
|
|
|
+ }
|
|
|
+ res.set("Content-Type", contentType);
|
|
|
+ res.status(status);
|
|
|
+ res.send(Buffer.from(data, "base64"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ let CAARes;
|
|
|
+ try {
|
|
|
+ CAARes = await this.axios.get(`https://coverartarchive.org/${path}`, {
|
|
|
+ responseType: "arraybuffer"
|
|
|
+ });
|
|
|
+ } catch (err) {
|
|
|
+ if (err.response) CAARes = err.response;
|
|
|
+ else {
|
|
|
+ console.log(`Non-normal error when requesting coverartarchive.org: ${err.message}`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const contentType = CAARes.headers["content-type"];
|
|
|
+ const data = Buffer.from(CAARes.data, "binary").toString("base64");
|
|
|
+ const { status } = CAARes;
|
|
|
+
|
|
|
+ await CacheModule.runJob("HSET", {
|
|
|
+ table: MUSICBRAINZ_CAA_CACHED_RESPONSE_REDIS_TABLE,
|
|
|
+ key: path,
|
|
|
+ value: JSON.stringify({
|
|
|
+ contentType,
|
|
|
+ data,
|
|
|
+ status
|
|
|
+ })
|
|
|
+ });
|
|
|
+
|
|
|
+ res.set("Content-Type", contentType);
|
|
|
+ res.status(status);
|
|
|
+ res.send(Buffer.from(data, "base64"));
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
/**
|