from
flask
import
Flask, render_template_string, request, jsonify, url_for
import
subprocess
import
win32com.client
import
pythoncom
import
configparser
import
os
app
=
Flask(__name__)
@app
.route(
'/open-folder'
, methods
=
[
'POST'
])
def
open_folder():
data
=
request.get_json()
folder_path
=
data.get(
'path'
)
if
not
folder_path:
return
jsonify({
"success"
:
False
,
"error"
:
"未提供路径"
}),
400
try
:
subprocess.Popen(f
'explorer "{folder_path}"'
)
return
jsonify({
"success"
:
True
})
except
Exception as e:
return
jsonify({
"success"
:
False
,
"error"
:
str
(e)})
def
html(data):
html_content
=
f
return
html_content
def
get_icon():
icons_dir
=
os.path.join(os.getcwd(),
"static/icons"
)
if
not
os.path.exists(icons_dir)
or
not
os.path.isdir(icons_dir):
print
(f
"目录不存在: {icons_dir}"
)
return
[]
file_list
=
[]
for
filename
in
os.listdir(icons_dir):
file_path
=
os.path.join(icons_dir, filename)
if
os.path.isfile(file_path):
file_list.append(filename.replace(
".ico"
, ""))
return
file_list
def
create_icon(file_paths, icon_list):
for
i
in
file_paths:
icon_path
=
i.replace(
'\\', '
\\\\')
icon_name
=
os.path.basename(i)
if
icon_name.replace(
".exe"
, "")
in
icon_list:
continue
if
icon_path.endswith(
".exe"
)
and
icon_name
not
in
[
"pycdas.exe"
,
"pycdc.exe"
,
"upx.exe"
]:
command
=
f
'icoextract "{icon_path}" ".\\static\\icons\\{icon_name.replace(".exe", ".ico")}"'
subprocess.Popen(command)
def
get_html_content():
config
=
configparser.ConfigParser()
config.read(
'setting.ini'
)
dir_path
=
config[
'DESKTOP_PATH'
][
'PATH'
]
dir_result
=
get_subdirectories(dir_path)
out_section
=
""
icon_list
=
get_icon()
for
i
in
range
(
len
(dir_result)):
out_app_section
=
""
app_result, app_result_ini
=
get_direct_files_and_dirs(dir_result[i])
create_icon(app_result, icon_list)
section_name
=
dir_result[i].split(
'\\'
)[
-
1
]
for
j
in
range
(
len
(app_result)):
app_name_ini
=
app_result_ini[j].split(
'\\'
)[
-
1
]
app_name
=
app_result[j].split(
'\\'
)[
-
1
]
app_path
=
app_result[j].replace(
'\\', '
\\\\')
if
app_name.endswith(
".exe"
):
ico_path
=
url_for(
'static'
, filename
=
f
'icons/{app_name.replace(".exe", ".ico")}'
)
elif
app_name.endswith(
".xls"
)
or
app_name.endswith(
".xlsx"
):
ico_path
=
url_for(
'static'
, filename
=
'default_icons/excel.ico'
)
elif
app_name.endswith(
".doc"
)
or
app_name.endswith(
".docx"
):
ico_path
=
url_for(
'static'
, filename
=
'default_icons/word.ico'
)
elif
app_name.endswith(
".ppt"
)
or
app_name.endswith(
".pptx"
):
ico_path
=
url_for(
'static'
, filename
=
'default_icons/ppt.ico'
)
elif
app_name.endswith(
".pdf"
):
ico_path
=
url_for(
'static'
, filename
=
'default_icons/pdf.ico'
)
elif
app_name.endswith(
".txt"
):
ico_path
=
url_for(
'static'
, filename
=
'default_icons/txt.ico'
)
elif
'.'
not
in
app_name:
ico_path
=
url_for(
'static'
, filename
=
'default_icons/dir.ico'
)
else
:
ico_path
=
url_for(
'static'
, filename
=
'default_icons/other.ico'
)
app_section
=
f
out_app_section
+
=
app_section
category_section
=
f
out_section
=
out_section
+
category_section
html_content
=
html(out_section)
return
html_content
@app
.route(
'/'
)
def
home():
html_content
=
get_html_content()
return
render_template_string(html_content)
def
get_subdirectories(directory):
sub_dirs
=
[]
for
name
in
os.listdir(directory):
full_path
=
os.path.join(directory, name)
if
os.path.isdir(full_path):
sub_dirs.append(full_path)
return
sub_dirs
def
get_direct_files_and_dirs(directory):
result
=
[]
result_ini
=
[]
pythoncom.CoInitialize()
try
:
shell
=
win32com.client.Dispatch(
"WScript.Shell"
)
except
Exception as e:
print
(f
"创建 WScript.Shell 失败: {e}"
)
return
result
for
name
in
os.listdir(directory):
full_path
=
os.path.join(directory, name)
if
os.path.isfile(full_path)
and
name.lower().endswith(
".lnk"
):
try
:
shortcut
=
shell.CreateShortcut(full_path)
target_path
=
shortcut.TargetPath
if
target_path
and
os.path.exists(target_path):
result.append(target_path)
except
Exception as e:
print
(f
"解析快捷方式失败: {full_path} - {e}"
)
else
:
result.append(full_path)
result_ini.append(full_path)
pythoncom.CoUninitialize()
return
result, result_ini
if
__name__
=
=
'__main__'
:
app.run(debug
=
True
)