74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
import json
|
||
|
|
import re
|
||
|
|
|
||
|
|
# 读取视觉设计项目案例数据
|
||
|
|
with open('网页未导入数据/视觉设计产业/视觉设计项目案例.json', 'r', encoding='utf-8') as f:
|
||
|
|
visual_projects = json.load(f)
|
||
|
|
|
||
|
|
# 读取mock文件
|
||
|
|
with open('src/mocks/projectLibraryMock.js.backup_attachments', 'r', encoding='utf-8') as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
# 创建项目名称到附件的映射
|
||
|
|
name_to_attachments = {}
|
||
|
|
for project in visual_projects:
|
||
|
|
name = project.get("案例名称", "")
|
||
|
|
attachments_str = project.get("附件", "")
|
||
|
|
|
||
|
|
if name and attachments_str:
|
||
|
|
# 分割附件字符串
|
||
|
|
files = [f.strip() for f in attachments_str.split(',')]
|
||
|
|
attachments_list = []
|
||
|
|
|
||
|
|
for file in files:
|
||
|
|
ext = file.split('.')[-1].lower()
|
||
|
|
# 判断文件类型
|
||
|
|
file_type = 'document'
|
||
|
|
if ext in ['jpg', 'jpeg', 'png', 'gif']:
|
||
|
|
file_type = 'image'
|
||
|
|
elif ext in ['mp4', 'avi', 'mov']:
|
||
|
|
file_type = 'video'
|
||
|
|
elif ext == 'pdf':
|
||
|
|
file_type = 'pdf'
|
||
|
|
elif ext == 'psd':
|
||
|
|
file_type = 'design'
|
||
|
|
elif ext in ['doc', 'docx']:
|
||
|
|
file_type = 'word'
|
||
|
|
|
||
|
|
attachments_list.append({
|
||
|
|
'name': file,
|
||
|
|
'type': file_type,
|
||
|
|
'url': f'/attachments/{file}'
|
||
|
|
})
|
||
|
|
|
||
|
|
name_to_attachments[name] = attachments_list
|
||
|
|
|
||
|
|
# 现在替换mock文件中每个项目的attachments字段
|
||
|
|
# 找到每个项目并替换其attachments内容
|
||
|
|
for name, attachments in name_to_attachments.items():
|
||
|
|
# 转义名称中的特殊字符
|
||
|
|
escaped_name = re.escape(name)
|
||
|
|
|
||
|
|
# 查找这个项目的位置
|
||
|
|
pattern = rf'"name": "{escaped_name}".*?"attachments": \[(.*?)\]'
|
||
|
|
|
||
|
|
# 生成新的attachments内容
|
||
|
|
attachments_json = json.dumps(attachments, ensure_ascii=False, indent=12)
|
||
|
|
# 调整缩进
|
||
|
|
attachments_json = attachments_json.replace('\n', '\n ')
|
||
|
|
attachments_json = attachments_json[1:-1] # 去掉外层的[]
|
||
|
|
|
||
|
|
# 替换attachments内容
|
||
|
|
replacement = lambda m: m.group(0).replace(
|
||
|
|
f'"attachments": [{m.group(1)}]',
|
||
|
|
f'"attachments": [\n {attachments_json}\n ]'
|
||
|
|
)
|
||
|
|
|
||
|
|
content = re.sub(pattern, replacement, content, flags=re.DOTALL)
|
||
|
|
|
||
|
|
# 写回文件
|
||
|
|
with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f:
|
||
|
|
f.write(content)
|
||
|
|
|
||
|
|
print("附件数据已成功修复!")
|