-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
99 lines (80 loc) · 2.41 KB
/
Program.cs
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
using System.Web;
using Microsoft.AspNetCore.HttpLogging;
using Swashbuckle.AspNetCore.Annotations;
using YTMP3DownloadAPI.Logger;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddLogging(logging =>
{
logging.ClearProviders();
logging.AddApplicationLogger();
});
// swagger service
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c => c.EnableAnnotations());
// Http request log service
builder.Services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.Request;
});
var app = builder.Build();
app.UseRequestLog();
// http://localhost:5195/swagger/index.html
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
// options.RoutePrefix = string.Empty;
options.DocumentTitle = "youtube mp3 download api document";
});
async Task<IResult> downloadWithvideoName(
[SwaggerParameter("youtube影片ID")]
string videoID
)
{
try
{
// parse url in route param videourl
var videoUrl = HttpUtility.UrlDecode($"https://www.youtube.com/watch?v={videoID}");
var serviceRes = await downloadService.download(videoUrl, null, null);
return Results.File(
serviceRes.fileStream,
contentType: "audio/mp3",
fileDownloadName: serviceRes.fileName
);
}
catch (System.Exception)
{
throw;
}
}
async Task<IResult?> downloadWithCustomName(
[SwaggerParameter("youtube影片ID")]
string videoID,
[SwaggerParameter("自訂檔名")]
string custName,
[SwaggerParameter("comment")]
string? comment
)
{
try
{
// parse url in route param videourl
var videoUrl = HttpUtility.UrlDecode($"https://www.youtube.com/watch?v={videoID}");
var serviceRes = await downloadService.download(videoUrl, custName, comment);
var fileDownloadName = serviceRes.fileName.Split("]").Last().Trim();
return Results.File(
serviceRes.fileStream,
contentType: "audio/mp3",
fileDownloadName: fileDownloadName
);
}
catch (System.Exception e)
{
throw;
}
}
// TODO: 做一個簡易的前端對外
app.MapGet("/download/{videoID}", downloadWithvideoName);
app.MapGet("/download/{videoID}/{custName}", downloadWithCustomName);
app.MapGet("/a", () => new { status = 200, message = "動了,它動了" });
app.Run();