Commit 9b142e47 authored by liujiangnan's avatar liujiangnan

feat: PDF转图片

parent fa23b016
......@@ -23,7 +23,7 @@
<div id="edit-data" class="edit-data">
<el-upload class="upload-demo" drag
:action="uploadPdfUrl"
:data="uploadPdfData"
data=""
:show-file-list="false"
:on-success="handleCoverSuccess"
:on-error="handleCoverError"
......@@ -32,7 +32,7 @@
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传pdf/ppt/word文件,且不超过100Mb</div>
<div class="el-upload__tip" slot="tip">只能上传pdf文件,且不超过100Mb</div>
</el-upload>
<!-- 添加进度条和取消按钮 -->
......
......@@ -13,33 +13,19 @@ new Vue({
created() {
window.courseware.getData(async (dt) => {
this.fileData = dt;
if (this.fileData && this.fileData.ppt_html) {
this.showErrorMsg = true;
this.showErrorMsg = await this.checkUrl(this.fileData.ppt_html);
if (!this.showErrorMsg && this.fileData.items.length == 0) {
await this.setPptImageItems();
}
}
},key);
window['air'].getUploadCallback = (url, data) => {
this.uploadUrl = url;
this.uploadCover = data;
this.uploadData = data;
};
setInterval(async () => {
if (this.fileData && this.fileData.ppt_html) {
this.showErrorMsg = await this.checkUrl(this.fileData.ppt_html);
if (!this.showErrorMsg && this.fileData.items.length == 0) {
await this.setPptImageItems();
}
}
}, 5000);
},
data() {
var protocolStr = document.location.protocol;
return {
//封面上传相关
fileData: null,
uploadPdfUrl: `${protocolStr}//opensource.iteachabc.com/pdfUpload`,
uploadUrl: window.courseware.uploadUrl(),
uploadCover: window.courseware.uploadData(),
uploadData: window.courseware.uploadData(),
......@@ -51,57 +37,6 @@ new Vue({
}
},
methods: {
async setPptImageItems() {
if (!this.fileData || !this.fileData.ppt_html) {
return;
}
let index = 1;
let url = this.fileData.ppt_html.replace("index.html", `pic/${index}.jpg`);
let flag = await this.checkUrl(url);
while (!flag) {
this.fileData.items.push({
text_en: '',
text_cn: '',
audio_url: '',
image_url: url,
audio_name: '',
audio_size: '',
audio_duration: '',
isPlaying: false,
});
index++;
url = this.fileData.ppt_html.replace("index.html", `pic/${index}.jpg`);
flag = await this.checkUrl(url);
}
this.save();
},
// 检查 URL 是否可访问
async checkUrl(url) {
try {
const response = await fetch(url);
const text = await response.text();
// 检查返回内容是否包含错误 XML
if (text.includes('<Error>') && text.includes('<Code>NoSuchKey</Code>')) {
return true;
} else {
return false;
}
} catch (error) {
console.error('检查URL时出错:', error);
return true;
}
},
getProxiedUrl (url) {
try {
if (document.domain == 'localhost') {
return url;
}
const urlObj = new URL(url);
return `${urlObj.pathname}${urlObj.search}${urlObj.hash}`;
} catch (e) {
return url;
}
},
customUpload(options) {
this.uploading = true;
this.uploadProgress = 0;
......@@ -142,7 +77,7 @@ new Vue({
this.xhr = null;
};
xhr.open('POST', this.uploadUrl, true);
xhr.open('POST', this.uploadPdfUrl, true);
xhr.send(formData);
},
cancelUpload() {
......@@ -160,11 +95,21 @@ new Vue({
this.fileData= {
name: file.name,
url: response.url,
ppt_html: response.ppt_html?response.ppt_html.replace("staging-", ""):"",
items: [],
};
if (response.imagesUrls){
this.fileData.items = response.imagesUrls.map(item => ({
text_en: '',
text_cn: '',
audio_url: '',
image_url: item,
audio_name: '',
audio_size: '',
audio_duration: '',
isPlaying: false,
}));
}
this.save();
this.showErrorMsg = false;
}
},
handleCoverError(err, file, fileList) {
......@@ -177,15 +122,15 @@ new Vue({
this.$message.warning('请等待当前文件上传完成或取消上传');
return false;
}
const isOffice = (file.type === 'application/pdf' || file.type.indexOf(`application/vnd.openxmlformats`) == 0);
const isPdf = (file.type === 'application/pdf');
const isLt2M = file.size / 1024 / 1024 / 1024 < 2;
if (!isOffice) {
this.$message.error('上传文件必须是PDF/Word/PPT!');
if (!isPdf) {
this.$message.error('上传文件必须是PDF!');
} else if (!isLt2M) {
this.$message.error('上传文件大小不能超过 200MB!');
}
return isOffice && isLt2M;
return isPdf && isLt2M;
},
save() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment