-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalcache.cpp
More file actions
75 lines (58 loc) · 2.37 KB
/
localcache.cpp
File metadata and controls
75 lines (58 loc) · 2.37 KB
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
#include "localcache.h"
// 缓存歌曲到本地
void LocalCache::cacheMusic(const Song& song) {
QString baseUrl = "https://gitee.com/MarkYangUp/music/raw/master";
QString songPath = song.getPath();
// 构建文件的完整 URL
QString mp3Url = QUrl(baseUrl + songPath + song.getMp3()).toEncoded(QUrl::FullyEncoded);
QString lyricUrl = QUrl(baseUrl + songPath + song.getLyric()).toEncoded(QUrl::FullyEncoded);
QString imgUrl = QUrl(baseUrl + songPath + song.getImg()).toEncoded(QUrl::FullyEncoded);
// 创建本地目录
QString localPath = "./localcache/" + songPath;
QDir().mkpath(localPath);
// 获取上级目录的路径
QString parentDir = QFileInfo(baseUrl + songPath + "music.json").dir().path();
// 移除最后一个路径部分,得到上一级目录的路径
QString grandParentDir = QFileInfo(parentDir).dir().path();
// 构建 music.json 的 URL
QString jsonUrl = QDir(grandParentDir).filePath("music.json");
jsonUrl = QUrl(jsonUrl).toEncoded(QUrl::FullyEncoded);
QString jsonPath = QDir(grandParentDir).filePath("music.json");
jsonPath.remove(0, 45);
jsonPath = "./localcache" + jsonPath;
downloadFile(jsonUrl, jsonPath);
// 下载并保存文件
downloadFile(mp3Url, localPath + "/" + song.getMp3());
downloadFile(lyricUrl, localPath + "/" + song.getLyric());
downloadFile(imgUrl, localPath + "/" + song.getImg());
emit addLocalMusic(song);
}
void LocalCache::downloadFile(const QString& fileUrl, const QString& outputPath) {
QNetworkAccessManager manager;
QNetworkReply* reply = manager.get(QNetworkRequest(QUrl(fileUrl)));
// 等待请求完成或超时
while (!reply->isFinished()) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 3000); // 处理事件循环,3000ms为超时时间
}
if (reply->error() == QNetworkReply::NoError) {
// 成功,保存文件
QFile file(outputPath);
if (file.open(QIODevice::WriteOnly)) {
file.write(reply->readAll());
file.close();
}
}
else {
// 处理错误
qDebug() << "文件下载出错:" << reply->errorString();
}
reply->deleteLater();
}
// 删除缓存中的歌曲文件
void LocalCache::deleteCachedMusic(const Song& song) {
QString cachePath = cacheFolderPath + "/" + song.getMusicName(); // 构建歌曲在缓存中的路径
QFile cachedMusic(cachePath);
if (cachedMusic.exists()) {
cachedMusic.remove();
}
}