119 lines
4.4 KiB
Plaintext
119 lines
4.4 KiB
Plaintext
class PluginServer
|
|
{
|
|
public Server;
|
|
public PluginServer(svr)
|
|
{
|
|
this.Server = svr;
|
|
}
|
|
public Handle(ctx)
|
|
{
|
|
if(ctx.Path == "/")
|
|
{
|
|
const conf = TYTD.Config["conf"];
|
|
if(TypeIsString(conf))
|
|
{
|
|
const jo = Json.Decode(conf);
|
|
if(TypeIsString(jo.url))
|
|
{
|
|
ctx.SendRedirect($"{jo.url.TrimEnd('/')}/",303);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
ctx.WithMimeType("text/html").SendText(<h1>Not configured</h1>);
|
|
return true;
|
|
}
|
|
else if(ctx.Path == "/artist-thumbnail")
|
|
{
|
|
const id = ctx.QueryParams.TryGetFirst("id");
|
|
if(TypeIsString(id))
|
|
{
|
|
if(!TYTD.Config.Directory.FileExists(/"ArtistThumbnails"/$"{id}.jpg"))
|
|
{
|
|
var resp = Net.Http.MakeRequest($"https://www.theaudiodb.com/api/v1/json/123/artist-mb.php?i={Net.Http.UrlEncode(id)}",{FollowRedirects=true});
|
|
if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
|
|
{
|
|
const url = resp.ReadAsJson().artists[0].strArtistThumb;
|
|
|
|
if(TypeIsString(url))
|
|
{
|
|
|
|
var resp2 = Net.Http.MakeRequest(url,{FollowRedirects=true});
|
|
if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
|
|
{
|
|
var strm=TYTD.Config.Directory.OpenFile(/"ArtistThumbnails"/$"{id}.jpg","wb");
|
|
resp2.CopyToStream(strm);
|
|
strm.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(TYTD.Config.Directory.FileExists(/"ArtistThumbnails"/$"{id}.jpg"))
|
|
{
|
|
var strm=TYTD.Config.Directory.OpenFile(/"ArtistThumbnails"/$"{id}.jpg","rb");
|
|
ctx.WithMimeType("image/jpg").SendStream(strm);
|
|
strm.Close();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
else if(ctx.Path == "/album-thumbnail")
|
|
{
|
|
const id = ctx.QueryParams.TryGetFirst("id");
|
|
if(TypeIsString(id))
|
|
{
|
|
if(!TYTD.Config.Directory.FileExists(/"AlbumThumbnails"/$"{id}.jpg"))
|
|
{
|
|
|
|
var resp2 = Net.Http.MakeRequest($"https://coverartarchive.org/release/{Net.Http.UrlEncode(id)}/front",{FollowRedirects=true});
|
|
if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
|
|
{
|
|
var strm=TYTD.Config.Directory.OpenFile(/"AlbumThumbnails"/$"{id}.jpg","wb");
|
|
resp2.CopyToStream(strm);
|
|
strm.Close();
|
|
}
|
|
|
|
|
|
}
|
|
if(TYTD.Config.Directory.FileExists(/"AlbumThumbnails"/$"{id}.jpg"))
|
|
{
|
|
var strm=TYTD.Config.Directory.OpenFile(/"AlbumThumbnails"/$"{id}.jpg","rb");
|
|
ctx.WithMimeType("image/jpg").SendStream(strm);
|
|
strm.Close();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
else if(ctx.Path == "/config")
|
|
{
|
|
if(ctx.Method == "GET")
|
|
{
|
|
const conf = TYTD.Config["conf"];
|
|
if(TypeIsString(conf))
|
|
{
|
|
const jo = Json.Decode(conf);
|
|
if(TypeIsString(jo.url))
|
|
{
|
|
ctx.WithMimeType("application/json").SendJson({url = jo.url});
|
|
return true;
|
|
}
|
|
}
|
|
|
|
ctx.WithMimeType("application/json").SendText("null");
|
|
return true;
|
|
|
|
}
|
|
else if(ctx.Method == "PUT")
|
|
{
|
|
TYTD.Config["conf"]=Json.Encode(ctx.ReadJson());
|
|
ctx.StatusCode = 204;
|
|
ctx.WriteHeaders();
|
|
this.SendStart();
|
|
return true;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
} |