Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
修复bug,优化前端
Browse files Browse the repository at this point in the history
  • Loading branch information
LanYunDev committed Feb 4, 2024
1 parent 8aa0971 commit 3a00dd0
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 466 deletions.
10 changes: 5 additions & 5 deletions api/vercel(未支持).go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"strings"

"csz.net/tgstate/conf"
"csz.net/tgstate/control"
"lanyundev/tgstate/conf"
"lanyundev/tgstate/control"
)

func Vercel(w http.ResponseWriter, r *http.Request) {
Expand All @@ -18,13 +18,13 @@ func Vercel(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
// 如果请求路径以 "/img/" 开头
if strings.HasPrefix(path, conf.FileRoute) {
control.D(w, r)
control.DownloadAPI(w, r)
return // 结束处理,确保不执行默认处理
}
switch path {
case "/api":
// 调用 control 包中的 UploadImageAPI 处理函数
control.Middleware(control.UploadImageAPI)(w, r)
// 调用 control 包中的 UploadAPI 处理函数
control.Middleware(control.UploadAPI)(w, r)
case "/pwd":
control.Pwd(w, r)
default:
Expand Down
5 changes: 2 additions & 3 deletions assets/templates/files.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<h1>上传文件到 Telegram</h1>
<label for="uploadFile" id="uploadFileLabel" class="custom-file-label">选择文件或拖拽到此区域</label>
<input type="file" name="image" id="uploadFile" class="custom-file-input" multiple>
<button id="uploadButton">上传</button>
<div id="loading">上传中...</div>
<button id="uploadButton">点击上传</button>
<div id="response" class="ui-widget"></div>
{{template "public/footer" .}}
{{template "public/footer" .}}
653 changes: 304 additions & 349 deletions assets/templates/footer.tmpl

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion assets/templates/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
color: #333;
}

p {
display:inline
}

.custom-file-input {
display: none;
}
Expand Down Expand Up @@ -86,7 +90,7 @@
.response-item {
margin-bottom: 10px;
padding: 10px;
border-radius: 5px;
border-radius: 10px;
}

.response-success {
Expand Down
1 change: 0 additions & 1 deletion assets/templates/images.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<h1>上传图片到 Telegram</h1><label for="uploadFile" id="uploadFileLabel" class="custom-file-label">选择图片</label> <input
type="file" name="image" id="uploadFile" accept=".jpg, .jpeg, .png" class="custom-file-input" multiple> <button
id="uploadButton">上传</button>
<div id="loading">上传中...</div>
<div id="response" class="ui-widget"></div>
{{template "public/footer" .}}
4 changes: 2 additions & 2 deletions assets/templates_min/files.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{template "public/header" .}}
<h1>上传文件到 Telegram</h1><label for=uploadFile id=uploadFileLabel class=custom-file-label>选择文件或拖拽到此区域</label> <input type=file name=image id=uploadFile class=custom-file-input multiple> <button id=uploadButton>上传</button><div id=loading>上传中...</div><div id=response class=ui-widget></div>
{{template "public/footer" .}}
<h1>上传文件到 Telegram</h1><label for=uploadFile id=uploadFileLabel class=custom-file-label>选择文件或拖拽到此区域</label><input type=file name=image id=uploadFile class=custom-file-input multiple><button id=uploadButton>点击上传</button><div id=response class=ui-widget></div>
{{template "public/footer" .}}
2 changes: 1 addition & 1 deletion assets/templates_min/footer.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{define "public/footer"}}
<script defer>const $uploadButton=document.getElementById("uploadButton"),$response=document.getElementById("response"),$loading=document.getElementById("loading"),$uploadFile=document.getElementById("uploadFile"),$uploadFileLabel=document.getElementById("uploadFileLabel"),$copyCodeButtons_text=".copy-code";function uploadFile(e){const t=20971520,n=e.name,o=e=>console.log(e),l=e=>{console.error(e);const t=`上传失败(${e.message})`;return $response.prepend(createResponseElement(t,"response-error")),Promise.reject("Upload failed")};if(e.size<=t)uploadImg(e,1).then(o).catch(l);else{let o,a=`tgstate-blob\n${n}`;const d=(l,s,r=0)=>{if(l<e.size){return uploadImg(e.slice(l,s),0,o).then((n=>(a+=`\n${n.substring(14)}`,l=s,s=Math.min(l+t,e.size),d(l,s))),(e=>{if(r<5)return o=`${n}上传块失败,2秒后重试 (${r+1}/5)`,console.warn(o),new Promise((e=>setTimeout(e,2e3))).then((()=>d(l,s,r+1)));throw console.error(`${n}上传块失败,已达到最大重试次数 (5)`),e}))}return Promise.resolve()};d(0,t).then((()=>{console.log(a);const e=new Blob([a],{type:"text/plain"});return uploadImg(new File([e],`${n}_fileAll.txt`,{type:"text/plain"}),1)})).catch(l)}}function uploadImg(e,t,n){const o=e.name;return new Promise(((l,a)=>{const d=new FormData;d.append("file",e);const s=e.type.startsWith("image/");$uploadButton.disabled=!0,$uploadButton.textContent=`${o}上传中`,$loading.style.display="block";const r=window.location.origin;fetch(`${r}/api`,{method:"POST",body:d,headers:{}}).then((e=>e.json())).then((e=>{const d=`${r}${e.message}?filename=${o}`,i=document.createElement("div");return 1===e.code?(t&&(s?(i.className="response-item response-success",i.innerHTML=`${o}上传成功,图片外链:<a target="_blank" href="${d}">${d}</a><div class="copy-links">\n <span class="copy-code" data-clipboard-text="&lt;img src=&quot;${d}&quot; alt=&quot;Your Alt Text&quot;&gt;">HTML</span>\n <span class="copy-code" data-clipboard-text="![Alt Text](${d})">Markdown</span>\n <span class="copy-code" data-clipboard-text="[img]${d}[/img]">BBCode</span>\n </div>`):(i.className="response-item response-success",i.innerHTML=`${o}上传成功,文件外链:<a target="_blank" href="${d}">${d}</a>`)),l(e.message)):(i.className="response-item response-error",i.textContent=n||`${o}上传失败(${e.message})`,a(`${o}上传失败(${e.message})`)),i.className&&$response.prepend(i),$uploadFile.value="",$uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF",document.querySelectorAll($copyCodeButtons_text).forEach((function(e){e.addEventListener("click",(function(){let e=this.dataset.clipboardText,t=document.createElement("input");document.body.appendChild(t),t.value=e,t.select(),document.execCommand("copy"),document.body.removeChild(t);let n=this,o=n.textContent;n.textContent="复制成功",setTimeout((function(){n.textContent=o}),500)}))})),e.message})).catch((e=>{let t=document.createElement("div");t.className="response-item response-error",t.textContent=`${o}上传失败`,$response.appendChild(t),a(`${o}上传失败`)})).finally((()=>{$uploadButton.disabled=!1,$uploadButton.textContent="上传",$loading.style.display="none"}))}))}function readAndUploadFile(e){const t=new FileReader;t.onload=function(t){const n=t.target.result;uploadFile(e,n)},t.readAsText(e)}function handleFileSelection(e){e.length>0?1===e.length?($uploadFileLabel.textContent="已选择文件: "+e[0].name,$uploadFileLabel.style.backgroundColor="#0056b3"):($uploadFileLabel.textContent="已选择多个文件",$uploadFileLabel.style.backgroundColor="#0056b3"):($uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF")}document.addEventListener("DOMContentLoaded",(function(){$uploadFile.addEventListener("change",(function(){handleFileSelection(this.files)})),$uploadButton.addEventListener("click",(function(){if($uploadFile.files.length>0)for(let e=0;e<$uploadFile.files.length;e++)readAndUploadFile($uploadFile.files[e]);else alert("请至少选择一个文件")}));let e=0;document.documentElement.addEventListener("dragover",(function(e){e.preventDefault()})),document.documentElement.addEventListener("dragenter",(function(t){t.preventDefault(),0===e&&(document.documentElement.style.backgroundColor="rgba(0, 123, 255, 0.1)"),e++})),document.documentElement.addEventListener("dragleave",(function(t){e--,0===e&&(document.documentElement.style.backgroundColor="transparent")})),document.documentElement.addEventListener("drop",(function(t){t.preventDefault(),document.documentElement.style.backgroundColor="transparent",$uploadFile.files=t.dataTransfer.files,handleFileSelection(t.dataTransfer.files),e=0}))}))</script>
<script defer>const $uploadButton=document.getElementById("uploadButton"),$response=document.getElementById("response"),$uploadFile=document.getElementById("uploadFile"),$uploadFileLabel=document.getElementById("uploadFileLabel"),$copyCodeButtons_text=".copy-code",errorClassName="response-item response-error",successClassName="response-item response-success",serverUrl=window.location.origin,serverUrl_API=`${serverUrl}/api`,SizeThreshold=20971520,retryCount=5,retryDelay=2e3,sleep=e=>new Promise((t=>setTimeout(t,e))),uploadFile=e=>{const t=e.name,o=e=>console.log(`${t}: ${serverUrl}${e}`),n=e=>($uploadButton.disabled=!1,$uploadButton.textContent="点击上传",console.error("上传失败! ",e),Promise.reject("上传失败! ",e));if(e.size<=20971520)handleFileUpload(e,1,t).then(o).catch(n);else{let l=`tgstate-blob\n${t}`,a=`${t}上传块第一次失败,将以每2秒重试(5)次!`;const s=(o,n,r=0)=>{if(o<e.size){const d=e.slice(o,n);return handleFileUpload(d,0,t,a).then((t=>(l+=`\n${t.substring(14)}`,o=n,n=Math.min(o+20971520,e.size),s(o,n))),(l=>r<5?(a=`${t}上传块失败,2秒后重试 (${r+1}/5)`,console.warn(a),creatErrorElement(e,t,a,l),sleep(2e3).then((()=>s(o,n,r+1)))):(a=`${t}上传失败!已达到最大重试次数(5)!`,console.error(a),creatErrorElement(e,t,a,l),Promise.reject(a))))}return Promise.resolve()};s(0,20971520).then((()=>{console.log(l);const e=new Blob([l],{type:"text/plain"}),o=new File([e],`${t}_fileAll.txt`,{type:"text/plain"});return handleFileUpload(o,1,t)})).then(o).catch(n)}},creatErrorElement=(e,t,o,n)=>{const l=`${t}_${e.size}_${e.lastModified}`,a=$response.querySelector(`.response-item.response-error[data-identifier="${l}"]`),s=o?`${o} (${n})`:`${t}上传失败! (${n})`;if(a)a.textContent=s;else{const e=document.createElement("div");e.className=errorClassName,e.textContent=s,$response.prepend(e)}},handleFileUpload=(e,t,o,n)=>new Promise(((l,a)=>{const s=new FormData;s.append("file",e),$uploadButton.disabled||($uploadButton.disabled=!0,$uploadButton.textContent=`${o}上传中`),fetch(serverUrl_API,{method:"POST",body:s,headers:{}}).then((e=>e.json())).then((n=>{if(1===n.code){if(l(n.message),t){const t=`${serverUrl}${n.message}?filename=${o}`,l=document.createElement("div"),a=e.type.startsWith("image/")?"图片":"文件";l.className=successClassName,l.innerHTML=`<p>${o}上传成功,${a}外链: </p><a target="_blank" href="${t}">${t}</a><div class="copy-links"><span class="copy-code" data-clipboard-text="&lt;img src=&quot;${t}&quot; alt=&quot;Your Alt Text&quot;&gt;">HTML</span><span class="copy-code" data-clipboard-text="![Alt Text](${t})">Markdown</span><span class="copy-code" data-clipboard-text="[img]${t}[/img]">BBCode</span><span class="copy-code" data-clipboard-text="${t}">复制外链</span></div>`,$response.prepend(l),$uploadFile.value="",$uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF",$uploadButton.disabled=!1,$uploadButton.textContent="点击上传"}}else a(n.message)})).catch((()=>{creatErrorElement(e,o,n,response.message)}))})),$copyCodeButtonsContainer=document.body;$copyCodeButtonsContainer.addEventListener("click",(function(e){const t=e.target.closest(".copy-code");if(t){const e=t.dataset.clipboardText,o=t.textContent;try{navigator.clipboard.writeText(e)}catch(e){console.error("使用Clipboard API复制失败:",e);const o=t.dataset.clipboardText,n=document.createElement("input");document.body.appendChild(n),n.value=o,n.select(),document.execCommand("copy"),document.body.removeChild(n)}t.textContent="复制成功",setTimeout((()=>t.textContent=o),500)}})),document.addEventListener("DOMContentLoaded",(function(){$uploadFile.addEventListener("change",(function(){handleFileSelection(this.files)})),$uploadButton.addEventListener("click",(function(){if($uploadFile.files.length>0)for(let e=0;e<$uploadFile.files.length;e++)uploadFile($uploadFile.files[e]);else alert("请至少选择一个文件")}));let e=0;document.documentElement.addEventListener("dragover",(function(e){e.preventDefault()})),document.documentElement.addEventListener("dragenter",(function(t){t.preventDefault(),0===e&&(document.documentElement.style.backgroundColor="rgba(0, 123, 255, 0.1)"),e++})),document.documentElement.addEventListener("dragleave",(function(t){e--,0===e&&(document.documentElement.style.backgroundColor="transparent")})),document.documentElement.addEventListener("drop",(function(t){t.preventDefault(),document.documentElement.style.backgroundColor="transparent",$uploadFile.files=t.dataTransfer.files,handleFileSelection($uploadFile.files),e=0}))}));const handleFileSelection=e=>{e.length>0?(1===e.length?$uploadFileLabel.textContent="已选择文件: "+e[0].name:$uploadFileLabel.textContent=`已选择${e.length}个文件`,$uploadFileLabel.style.backgroundColor="#0056b3"):($uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF")}</script>
{{end}}
2 changes: 1 addition & 1 deletion assets/templates_min/header.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{define "public/header"}}
<!doctype html><meta charset=UTF-8><title>tgState</title><meta name=viewport content="width=device-width,initial-scale=1,maximum-scale=1"><style>#uploadFileLabel{width:280px;height:100px;border:2.5px dashed #ccc;border-radius:20px;text-align:center;padding:20px;font-size:20px;margin:20px auto;cursor:pointer;display:block;background-color:#007bff;color:#fff}#uploadButton{width:260px;border:2.5px dashed #ccc;border-radius:20px;padding:20px;margin:20px auto;cursor:pointer;font-size:20px;display:block;background-color:#007bff;color:#fff}body{text-align:center}h1{color:#333}.custom-file-input{display:none}.custom-file-label{background-color:#007bff;color:#fff;padding:10px 20px;cursor:pointer}.custom-file-label:hover{background-color:#0056b3}#uploadButton{background-color:#007bff;color:#fff;padding:10px 20px;border:none;cursor:pointer}#uploadButton[disabled]{background-color:#ccc;cursor:not-allowed}#uploadButton:hover{background-color:#0056b3}#response{margin-top:20px;padding:10px}.response-item{margin-bottom:10px;padding:10px;border-radius:5px}.response-success{background-color:#d4edda;border-color:#c3e6cb;color:#155724}.response-error{background-color:#f8d7da;border-color:#f5c6cb;color:#721c24}#loading{display:none}.copy-code{margin:5px}.copy-links{margin-top:5px}#uploadButton[disabled]:hover{background-color:#ccc;cursor:not-allowed}.password{margin:0;padding:0;display:flex;justify-content:center;align-items:center;height:100vh;background-color:#f2f2f2}.form-container{text-align:center;background-color:#fff;padding:20px;border-radius:10px;box-shadow:0 0 10px rgba(0,0,0,.2)}.form-input{width:300px;padding:10px;margin:10px;border:1px solid #ccc;border-radius:5px;font-size:16px}.form-button{padding:10px 20px;background-color:#007bff;color:#fff;border:none;border-radius:5px;font-size:18px;cursor:pointer}@media (max-width:465px){.form-container{padding:0;border-radius:0}.form-input{margin-top:30px}}</style>
<!doctype html><meta charset=UTF-8><title>tgState</title><meta name=viewport content="width=device-width,initial-scale=1,maximum-scale=1"><style>#uploadFileLabel{width:280px;height:100px;border:2.5px dashed #ccc;border-radius:20px;text-align:center;padding:20px;font-size:20px;margin:20px auto;cursor:pointer;display:block;background-color:#007bff;color:#fff}#uploadButton{width:260px;border:2.5px dashed #ccc;border-radius:20px;padding:20px;margin:20px auto;cursor:pointer;font-size:20px;display:block;background-color:#007bff;color:#fff}body{text-align:center}h1{color:#333}p{display:inline}.custom-file-input{display:none}.custom-file-label{background-color:#007bff;color:#fff;padding:10px 20px;cursor:pointer}.custom-file-label:hover{background-color:#0056b3}#uploadButton{background-color:#007bff;color:#fff;padding:10px 20px;border:none;cursor:pointer}#uploadButton[disabled]{background-color:#ccc;cursor:not-allowed}#uploadButton:hover{background-color:#0056b3}#response{margin-top:20px;padding:10px}.response-item{margin-bottom:10px;padding:10px;border-radius:10px}.response-success{background-color:#d4edda;border-color:#c3e6cb;color:#155724}.response-error{background-color:#f8d7da;border-color:#f5c6cb;color:#721c24}#loading{display:none}.copy-code{margin:5px}.copy-links{margin-top:5px}#uploadButton[disabled]:hover{background-color:#ccc;cursor:not-allowed}.password{margin:0;padding:0;display:flex;justify-content:center;align-items:center;height:100vh;background-color:#f2f2f2}.form-container{text-align:center;background-color:#fff;padding:20px;border-radius:10px;box-shadow:0 0 10px rgba(0,0,0,.2)}.form-input{width:300px;padding:10px;margin:10px;border:1px solid #ccc;border-radius:5px;font-size:16px}.form-button{padding:10px 20px;background-color:#007bff;color:#fff;border:none;border-radius:5px;font-size:18px;cursor:pointer}@media (max-width:465px){.form-container{padding:0;border-radius:0}.form-input{margin-top:30px}}</style>
{{end}}
1 change: 0 additions & 1 deletion assets/templates_min/images.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<h1>上传图片到 Telegram</h1><label for="uploadFile" id="uploadFileLabel" class="custom-file-label">选择图片</label> <input
type="file" name="image" id="uploadFile" accept=".jpg, .jpeg, .png" class="custom-file-input" multiple> <button
id="uploadButton">上传</button>
<div id="loading">上传中...</div>
<div id="response" class="ui-widget"></div>
{{template "public/footer" .}}
Loading

0 comments on commit 3a00dd0

Please # to comment.