-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDownloadsource.cs
154 lines (133 loc) · 5.57 KB
/
Downloadsource.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
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
using System.Text;
using RestSharp;
class Downloadsource
{
private static readonly RestClient client = new RestClient();
public static async Task DownloadsourceMain(string[] args)
{
try
{
// 設定 Console 輸出編碼(如需支援中文或 UTF-8)
Console.OutputEncoding = Encoding.UTF8;
// 讀取基本 URL(假設已存在一個 AddressablesCatalogUrlRoot.txt )
string addressablesCatalogUrlRootPath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Downloads",
"XAPK",
"Processed",
"AddressablesCatalogUrlRoot.txt"
);
string baseURL = File.ReadAllText(addressablesCatalogUrlRootPath).Trim();
// 準備要下載的檔案對應
var fileMappings = new Dictionary<string, string>
{
{ "TableCatalog.bytes", "TableBundles/TableCatalog.bytes" },
{ "bundleDownloadInfo.json", "Android/bundleDownloadInfo.json" },
{ "MediaCatalog.bytes", "MediaResources/Catalog/MediaCatalog.bytes" }
};
// 建立一個串列來裝所有下載任務
var downloadTasks = new List<Task>();
// 逐一建立下載工作
foreach (var fileMapping in fileMappings)
{
string fileUrl = $"{baseURL}/{fileMapping.Value}";
string localFilePath = GetLocalFilePath(fileMapping.Key);
Console.WriteLine($"Preparing to download file: {fileUrl}");
Console.WriteLine($"Local file path: {localFilePath}");
// 如果本地檔案已經存在,刪除
if (File.Exists(localFilePath))
{
File.Delete(localFilePath);
Console.WriteLine($"File {localFilePath} already exists, deleted.");
}
else
{
Console.WriteLine($"File {localFilePath} does not exist, no need to delete.");
}
// 新增非同步下載任務
downloadTasks.Add(DownloadFileWithRestSharp(fileUrl, localFilePath));
}
// 等待所有下載執行完畢
Console.WriteLine("Waiting for all downloads to complete...");
await Task.WhenAll(downloadTasks);
// 確認檔案是否都下載成功
bool allFilesExist = true;
foreach (var fileMapping in fileMappings)
{
string localFilePath = GetLocalFilePath(fileMapping.Key);
if (!File.Exists(localFilePath))
{
Console.WriteLine($"File {localFilePath} does not exist.");
allFilesExist = false;
}
}
if (allFilesExist)
{
Console.WriteLine("All files downloaded successfully.");
await Processedbytes.ProcessedbytesMain(args);
}
else
{
Console.WriteLine("Downloaded files are incomplete, please check and retry.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
}
private static async Task DownloadFileWithRestSharp(string fileUrl, string localFilePath)
{
try
{
Console.WriteLine($"Starting to download file: {fileUrl}");
var request = new RestRequest(fileUrl, Method.Get);
RestResponse response = await client.ExecuteAsync(request);
// 檢查是否下載成功
if (response.IsSuccessful)
{
Console.WriteLine($"Processing file path: {localFilePath}");
string directoryPath = Path.GetDirectoryName(localFilePath);
if (!Directory.Exists(directoryPath))
{
Console.WriteLine($"Directory does not exist, creating: {directoryPath}");
Directory.CreateDirectory(directoryPath);
}
Console.WriteLine($"Writing content to file: {localFilePath}");
await File.WriteAllBytesAsync(localFilePath, response.RawBytes);
Console.WriteLine($"File {localFilePath} downloaded successfully.");
}
else
{
Console.WriteLine($"File {localFilePath} download failed, error code: {response.StatusCode}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while downloading file {fileUrl}: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
}
private static string GetLocalFilePath(string fileName)
{
try
{
// 依副檔名決定存放在 bytes/ 或 json/ 資料夾
string subDirectory = fileName.EndsWith(".bytes") ? "bytes" : "json";
string localFilePath = Path.Combine("Downloads", subDirectory, fileName);
string directoryPath = Path.GetDirectoryName(localFilePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
return localFilePath;
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while generating local file path: {ex.Message}");
Console.WriteLine(ex.StackTrace);
throw;
}
}
}