改用API驗證
This commit is contained in:
@@ -219,11 +219,27 @@ export const useJobsStore = defineStore('jobs', {
|
||||
async downloadFile(jobUuid, languageCode, filename) {
|
||||
try {
|
||||
const response = await filesAPI.downloadFile(jobUuid, languageCode)
|
||||
|
||||
// 使用 FileSaver.js 下載檔案
|
||||
const blob = new Blob([response], { type: 'application/octet-stream' })
|
||||
|
||||
// 根據檔案副檔名設定正確的MIME類型
|
||||
const getFileType = (filename) => {
|
||||
const ext = filename.toLowerCase().split('.').pop()
|
||||
const mimeTypes = {
|
||||
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'doc': 'application/msword',
|
||||
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'xls': 'application/vnd.ms-excel',
|
||||
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
'pdf': 'application/pdf',
|
||||
'txt': 'text/plain',
|
||||
'zip': 'application/zip'
|
||||
}
|
||||
return mimeTypes[ext] || 'application/octet-stream'
|
||||
}
|
||||
|
||||
// 使用 FileSaver.js 下載檔案,使用正確的MIME類型
|
||||
const blob = new Blob([response], { type: getFileType(filename) })
|
||||
saveAs(blob, filename)
|
||||
|
||||
|
||||
ElMessage.success('檔案下載完成')
|
||||
} catch (error) {
|
||||
console.error('下載檔案失敗:', error)
|
||||
|
@@ -464,8 +464,9 @@ const viewJobDetail = (jobUuid) => {
|
||||
const downloadJob = async (job) => {
|
||||
try {
|
||||
if (job.target_languages.length === 1) {
|
||||
const ext = getFileExtension(job.original_filename)
|
||||
const filename = `${job.original_filename.replace(/\.[^/.]+$/, '')}_${job.target_languages[0]}_translated.${ext}`
|
||||
const originalExt = getFileExtension(job.original_filename)
|
||||
const translatedExt = getTranslatedFileExtension(originalExt)
|
||||
const filename = `${job.original_filename.replace(/\.[^/.]+$/, '')}_${job.target_languages[0]}_translated.${translatedExt}`
|
||||
await jobsStore.downloadFile(job.job_uuid, job.target_languages[0], filename)
|
||||
} else {
|
||||
const filename = `${job.original_filename.replace(/\.[^/.]+$/, '')}_translated.zip`
|
||||
@@ -505,6 +506,15 @@ const getFileExtension = (filename) => {
|
||||
return filename.split('.').pop().toLowerCase()
|
||||
}
|
||||
|
||||
const getTranslatedFileExtension = (originalExt) => {
|
||||
// PDF 翻譯後變成 DOCX
|
||||
if (originalExt === 'pdf') {
|
||||
return 'docx'
|
||||
}
|
||||
// 其他格式保持不變
|
||||
return originalExt
|
||||
}
|
||||
|
||||
const formatFileSize = (bytes) => {
|
||||
if (bytes === 0) return '0 B'
|
||||
|
||||
|
@@ -232,7 +232,9 @@ const handleJobAction = async (action, job) => {
|
||||
case 'download':
|
||||
// 如果只有一個目標語言,直接下載
|
||||
if (job.target_languages.length === 1) {
|
||||
const filename = `${job.original_filename.replace(/\.[^/.]+$/, '')}_${job.target_languages[0]}_translated.${getFileExtension(job.original_filename)}`
|
||||
const originalExt = getFileExtension(job.original_filename)
|
||||
const translatedExt = getTranslatedFileExtension(originalExt)
|
||||
const filename = `${job.original_filename.replace(/\.[^/.]+$/, '')}_${job.target_languages[0]}_translated.${translatedExt}`
|
||||
await jobsStore.downloadFile(job.job_uuid, job.target_languages[0], filename)
|
||||
} else {
|
||||
// 多個語言,下載打包檔案
|
||||
@@ -301,6 +303,15 @@ const getFileExtension = (filename) => {
|
||||
return filename.split('.').pop().toLowerCase()
|
||||
}
|
||||
|
||||
const getTranslatedFileExtension = (originalExt) => {
|
||||
// PDF 翻譯後變成 DOCX
|
||||
if (originalExt === 'pdf') {
|
||||
return 'docx'
|
||||
}
|
||||
// 其他格式保持不變
|
||||
return originalExt
|
||||
}
|
||||
|
||||
const formatFileSize = (bytes) => {
|
||||
if (bytes === 0) return '0 B'
|
||||
|
||||
|
@@ -315,26 +315,26 @@
|
||||
:key="`${file.file_type}_${file.language_code || 'original'}`"
|
||||
class="file-item"
|
||||
>
|
||||
<div class="file-icon" :class="getFileExtension(file.filename)">
|
||||
{{ getFileExtension(file.filename).toUpperCase() }}
|
||||
<div class="file-icon" :class="getFileExtension(file.original_filename)">
|
||||
{{ getFileExtension(file.original_filename).toUpperCase() }}
|
||||
</div>
|
||||
<div class="file-info">
|
||||
<div class="file-name">{{ file.filename }}</div>
|
||||
<div class="file-name">{{ file.original_filename }}</div>
|
||||
<div class="file-details">
|
||||
<span class="file-size">{{ formatFileSize(file.file_size) }}</span>
|
||||
<span class="file-type">
|
||||
{{ file.file_type === 'ORIGINAL' ? '原始檔案' :
|
||||
{{ file.file_type === 'source' ? '原始檔案' :
|
||||
file.language_code === 'combined' ? '組合翻譯檔案 (多語言)' :
|
||||
`翻譯檔案 (${getLanguageText(file.language_code)})` }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="file-actions">
|
||||
<el-button
|
||||
v-if="file.file_type === 'TRANSLATED'"
|
||||
<el-button
|
||||
v-if="file.file_type === 'translated'"
|
||||
type="primary"
|
||||
size="small"
|
||||
@click="file.language_code === 'combined' ? downloadCombinedFile() : downloadFile(file.language_code, file.filename)"
|
||||
@click="file.language_code === 'combined' ? downloadCombinedFile() : downloadFile(file.language_code, file.original_filename)"
|
||||
>
|
||||
<el-icon><Download /></el-icon>
|
||||
下載
|
||||
@@ -388,9 +388,9 @@ const jobUuid = computed(() => route.params.uuid)
|
||||
|
||||
// 檢查是否有combined檔案
|
||||
const hasCombinedFile = computed(() => {
|
||||
return jobFiles.value.some(file =>
|
||||
file.language_code === 'combined' ||
|
||||
file.filename.toLowerCase().includes('combine')
|
||||
return jobFiles.value.some(file =>
|
||||
file.language_code === 'combined' ||
|
||||
(file.original_filename && file.original_filename.toLowerCase().includes('combine'))
|
||||
)
|
||||
})
|
||||
|
||||
@@ -452,8 +452,10 @@ const handleAction = async (command) => {
|
||||
|
||||
const downloadFile = async (langCode, customFilename = null) => {
|
||||
try {
|
||||
const ext = getFileExtension(job.value.original_filename)
|
||||
const filename = customFilename || `${job.value.original_filename.replace(/\.[^/.]+$/, '')}_${langCode}_translated.${ext}`
|
||||
// 根據原始文件類型決定翻譯後的副檔名
|
||||
const originalExt = getFileExtension(job.value.original_filename)
|
||||
const translatedExt = getTranslatedFileExtension(originalExt)
|
||||
const filename = customFilename || `${job.value.original_filename.replace(/\.[^/.]+$/, '')}_${langCode}_translated.${translatedExt}`
|
||||
await jobsStore.downloadFile(jobUuid.value, langCode, filename)
|
||||
} catch (error) {
|
||||
console.error('下載檔案失敗:', error)
|
||||
@@ -476,7 +478,7 @@ const downloadCombinedFile = async () => {
|
||||
} else {
|
||||
// 使用預設檔名或從任務資料獲取
|
||||
const originalName = job.value.original_filename
|
||||
if (originalName) {
|
||||
if (originalName && typeof originalName === 'string') {
|
||||
const nameParts = originalName.split('.')
|
||||
const baseName = nameParts.slice(0, -1).join('.')
|
||||
const extension = nameParts[nameParts.length - 1]
|
||||
@@ -507,7 +509,8 @@ const downloadCombinedFile = async () => {
|
||||
|
||||
const downloadAllFiles = async () => {
|
||||
try {
|
||||
const filename = `${job.value.original_filename.replace(/\.[^/.]+$/, '')}_translated.zip`
|
||||
const originalName = job.value.original_filename || 'translated_files'
|
||||
const filename = `${originalName.replace(/\.[^/.]+$/, '')}_translated.zip`
|
||||
await jobsStore.downloadAllFiles(jobUuid.value, filename)
|
||||
} catch (error) {
|
||||
console.error('批量下載失敗:', error)
|
||||
@@ -515,9 +518,19 @@ const downloadAllFiles = async () => {
|
||||
}
|
||||
|
||||
const getFileExtension = (filename) => {
|
||||
if (!filename || typeof filename !== 'string') return 'file'
|
||||
return filename.split('.').pop().toLowerCase()
|
||||
}
|
||||
|
||||
const getTranslatedFileExtension = (originalExt) => {
|
||||
// PDF 翻譯後變成 DOCX
|
||||
if (originalExt === 'pdf') {
|
||||
return 'docx'
|
||||
}
|
||||
// 其他格式保持不變
|
||||
return originalExt
|
||||
}
|
||||
|
||||
const formatFileSize = (bytes) => {
|
||||
if (bytes === 0) return '0 B'
|
||||
|
||||
|
@@ -405,8 +405,9 @@ const handleJobAction = async (action, job) => {
|
||||
try {
|
||||
if (job.target_languages.length === 1) {
|
||||
// 單一語言直接下載
|
||||
const ext = getFileExtension(job.original_filename)
|
||||
const filename = `${job.original_filename.replace(/\.[^/.]+$/, '')}_${job.target_languages[0]}_translated.${ext}`
|
||||
const originalExt = getFileExtension(job.original_filename)
|
||||
const translatedExt = getTranslatedFileExtension(originalExt)
|
||||
const filename = `${job.original_filename.replace(/\.[^/.]+$/, '')}_${job.target_languages[0]}_translated.${translatedExt}`
|
||||
await jobsStore.downloadFile(job.job_uuid, job.target_languages[0], filename)
|
||||
} else {
|
||||
// 多語言打包下載
|
||||
@@ -474,6 +475,15 @@ const getFileExtension = (filename) => {
|
||||
return filename.split('.').pop().toLowerCase()
|
||||
}
|
||||
|
||||
const getTranslatedFileExtension = (originalExt) => {
|
||||
// PDF 翻譯後變成 DOCX
|
||||
if (originalExt === 'pdf') {
|
||||
return 'docx'
|
||||
}
|
||||
// 其他格式保持不變
|
||||
return originalExt
|
||||
}
|
||||
|
||||
const formatFileSize = (bytes) => {
|
||||
if (bytes === 0) return '0 B'
|
||||
|
||||
|
Reference in New Issue
Block a user