ow
This commit is contained in:
1 parent
4f57b65451
commit
eae4296980
6 files changed
+722
-259
No files matched your search
@@ -386,7 +386,90 @@ namespace CloudBuilder.Master.Service
|
||||
dtl.OrderBy = dtls.Sum(x => x.OrderBy) + 1;
|
||||
dtl.Url = dmsFile.OrderKey;
|
||||
|
||||
// 同名的歌词、封面一起存下来(优先同目录)
|
||||
FillLyricAndPic(dmsFile, dtl);
|
||||
|
||||
repositoryPublishAudioDetail.InsertNow(dtl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 找出与音频同名的歌词和封面文件,写进明细。
|
||||
///
|
||||
/// 约定:音频叫 xxx.mp3,配套文件就是同目录下的 xxx.lrc(歌词)、xxx.png/jpg(封面)。
|
||||
/// 匹配用 OrgFileName 的主文件名(它是用户看到的原始文件名,如 song.mp3),
|
||||
/// 存的是 OrderKey——因为前端访问文件用的是 OrderKey(分片合并后的名字,如 uid-1-1.mp3)。
|
||||
/// 优先取同目录(FilePath 相同)的;同目录没有时才退而求其次找其它目录同名文件。
|
||||
/// </summary>
|
||||
private void FillLyricAndPic(DmsFileUploadViewEntity dmsFile, PublishAudioDetailEntity dtl)
|
||||
{
|
||||
if (dmsFile == null || dtl == null) return;
|
||||
if (string.IsNullOrEmpty(dmsFile.OrgFileName)) return;
|
||||
|
||||
string baseName = Path.GetFileNameWithoutExtension(dmsFile.OrgFileName);
|
||||
if (string.IsNullOrEmpty(baseName)) return;
|
||||
|
||||
IRepository<DmsFileUploadViewEntity> repositoryDmsFile = repository.GetRepository<IRepository<DmsFileUploadViewEntity>>();
|
||||
|
||||
// 先取回同目录的记录,文件名比对放在内存里做:
|
||||
// Path.GetFileNameWithoutExtension 翻不成 SQL,直接写进 Where 会运行时报错
|
||||
DmsFileUploadViewEntity[] inFolder = repositoryDmsFile.DetachedEntities
|
||||
.Where(x => x.FilePath == dmsFile.FilePath)
|
||||
.ToArray();
|
||||
|
||||
DmsFileUploadViewEntity lyric = FindCompanion(inFolder, baseName, true);
|
||||
DmsFileUploadViewEntity pic = FindCompanion(inFolder, baseName, false);
|
||||
|
||||
// 同目录凑不齐,再放宽到其它目录
|
||||
if (lyric == null || pic == null)
|
||||
{
|
||||
DmsFileUploadViewEntity[] others = repositoryDmsFile.DetachedEntities
|
||||
.Where(x => x.FilePath != dmsFile.FilePath)
|
||||
.ToArray();
|
||||
|
||||
if (lyric == null) lyric = FindCompanion(others, baseName, true);
|
||||
if (pic == null) pic = FindCompanion(others, baseName, false);
|
||||
}
|
||||
|
||||
if (lyric != null) dtl.Lrc = lyric.OrderKey;
|
||||
if (pic != null) dtl.Pic = pic.OrderKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在候选文件里找配得上 baseName 的歌词 / 图片。
|
||||
/// 类型用 FileTypePolicy 判断,避免自己维护一份扩展名清单。
|
||||
///
|
||||
/// 匹配优先级:
|
||||
/// 1) 完全同名:song.mp3 配 song.lrc,最稳妥,命中就直接用
|
||||
/// 2) 音频名包含配套文件名:歌手-歌名.mp3 也能配上 歌名.lrc
|
||||
/// 都用 OrdinalIgnoreCase,避免 Song.LRC / song.lrc 因大小写漏配。
|
||||
/// </summary>
|
||||
private DmsFileUploadViewEntity FindCompanion(DmsFileUploadViewEntity[] candidates, string baseName, bool wantLyric)
|
||||
{
|
||||
if (candidates == null || candidates.Length == 0) return null;
|
||||
|
||||
// 包含匹配的结果先存着,等确认没有完全同名的再用它
|
||||
DmsFileUploadViewEntity fuzzy = null;
|
||||
|
||||
foreach (DmsFileUploadViewEntity item in candidates)
|
||||
{
|
||||
if (item == null) continue;
|
||||
if (string.IsNullOrEmpty(item.OrgFileName) || string.IsNullOrEmpty(item.OrderKey)) continue;
|
||||
|
||||
string name = Path.GetFileNameWithoutExtension(item.OrgFileName);
|
||||
if (string.IsNullOrEmpty(name)) continue;
|
||||
|
||||
bool typeMatched = wantLyric
|
||||
? FileTypePolicy.IsLyric(item.OrgFileName)
|
||||
: FileTypePolicy.IsPhoto(item.OrgFileName);
|
||||
if (!typeMatched) continue;
|
||||
|
||||
if (string.Equals(name, baseName, StringComparison.OrdinalIgnoreCase)) return item;
|
||||
|
||||
if (fuzzy == null && baseName.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
fuzzy = item;
|
||||
}
|
||||
|
||||
return fuzzy;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user