mirror of
https://github.com/zhongyang219/MusicPlayer2.git
synced 2026-10-24 10:10:36 +08:00
Merge pull request #176 from lrisora/cover_relative_path
自定义外部图片文件名支持相对路径
This commit is contained in:
@@ -455,8 +455,20 @@ void CAppearanceSettingDlg::OnOK()
|
||||
|
||||
CString temp;
|
||||
GetDlgItemText(IDC_DEFAULT_COVER_NAME_EDIT, temp);
|
||||
temp.Replace(L'/', L'\\');
|
||||
CCommon::StringSplit(wstring(temp), L',', m_data.default_album_name);
|
||||
|
||||
for (auto& album_name : m_data.default_album_name)
|
||||
{
|
||||
// 虽然开头的.\\不是后续识别必须的但是如果发现缺少还是加上
|
||||
// 这是一个非简写的相对路径,绝对路径此处不做处理
|
||||
if (album_name.find(L'\\') != wstring::npos && album_name.front() != L'\\' && !CCommon::IsWindowsPath(album_name))
|
||||
{
|
||||
// 确保相对路径以".\\"开头
|
||||
if (album_name.at(0) == L'.' && album_name.at(1) == L'\\')
|
||||
continue;
|
||||
album_name = L".\\" + album_name;
|
||||
}
|
||||
}
|
||||
m_data.ui_refresh_interval = m_ui_refresh_interval_edit.GetValue();
|
||||
|
||||
//CTabDlg::OnOK();
|
||||
|
||||
+11
-13
@@ -4,6 +4,7 @@
|
||||
#include "FilePathHelper.h"
|
||||
#include <random>
|
||||
#include <functional>
|
||||
// #include <pathcch.h>
|
||||
|
||||
CCommon::CCommon()
|
||||
{
|
||||
@@ -758,22 +759,19 @@ _tstring CCommon::FileRename(const _tstring& file_path, const _tstring& new_file
|
||||
|
||||
_tstring CCommon::RelativePathToAbsolutePath(const _tstring & relative_path, const _tstring & cur_dir)
|
||||
{
|
||||
//
|
||||
// relative_path如果是盘符开头的绝对路径那么返回此绝对路径
|
||||
// 否则将relative_path视为相对路径或斜杠开头的绝对路径并与cur_dir拼接
|
||||
_tstring result = relative_path;
|
||||
_tstring dir = cur_dir;
|
||||
if (dir.empty() || dir.back() != _T('\\') || dir.back() != _T('/'))
|
||||
dir.push_back(_T('\\'));
|
||||
if ((result.size() > 0 && (result.front() == _T('.') || result.front() == _T('\\') || result.front() == _T('/')))) //如果是相对路径
|
||||
if (!IsWindowsPath(result) && !dir.empty())
|
||||
{
|
||||
if (result.front() == _T('.') && (result.size() == 1 || result[1] != _T('.'))) //如果路径前面是一个点而不是两个点,则把点去掉
|
||||
result = result.substr(1);
|
||||
if (result.size() > 0 && (result.front() == _T('\\') || result.front() == _T('/')))
|
||||
result = result.substr(1);
|
||||
result = dir + result;
|
||||
}
|
||||
else if (!IsWindowsPath(result))
|
||||
{
|
||||
result = dir + result;
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/
|
||||
// PathCombine拼接简化两个合法路径,当result为斜杠开头的绝对路径时将其只与dir的盘符拼接
|
||||
// PathCchCombine 是处理了缓冲区溢出的安全版本,Windows 8开始支持,位于pathcch.h
|
||||
TCHAR lpBuffer[MAX_PATH]{};
|
||||
PathCombine(lpBuffer, dir.c_str(), result.c_str());
|
||||
// PathCchCombine(lpBuffer, MAX_PATH, dir.c_str(), result.c_str());
|
||||
result = lpBuffer;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -441,22 +441,28 @@ std::wstring CMusicPlayerCmdHelper::SearchAlbumCover(const SongInfo& song)
|
||||
file_name = dir + album_name + L".*";
|
||||
CCommon::GetImageFiles(file_name, files);
|
||||
}
|
||||
//没有找到唱片集为文件名的文件,查找文件名为设置的专辑封面名的文件
|
||||
if (theApp.m_app_setting_data.use_out_image)
|
||||
if (!files.empty())
|
||||
album_cover_path = dir + files[0];
|
||||
// 没有找到唱片集为文件名的文件,查找文件名为设置的专辑封面名的文件
|
||||
else if (theApp.m_app_setting_data.use_out_image)
|
||||
{
|
||||
for (const auto& album_name : theApp.m_app_setting_data.default_album_name)
|
||||
{
|
||||
if (!files.empty())
|
||||
break;
|
||||
if (!album_name.empty())
|
||||
{
|
||||
file_name = dir + album_name + L".*";
|
||||
file_name = CCommon::RelativePathToAbsolutePath(album_name + L".*", dir);
|
||||
CCommon::GetImageFiles(file_name, files);
|
||||
}
|
||||
if (!files.empty())
|
||||
{
|
||||
// 处理album_name可能含有相对路径的情况,files[0]仅有文件名
|
||||
// 由于album_name中文件名部分可能含有通配符所以不能只替换后缀,需要替换整个文件名
|
||||
size_t index = file_name.rfind('\\');
|
||||
album_cover_path = file_name.substr(0, index + 1) + files[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!files.empty())
|
||||
album_cover_path = dir + files[0];
|
||||
}
|
||||
return album_cover_path;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user