mirror of
https://github.com/oneclickvirt/ecs.git
synced 2025-10-05 23:37:07 +08:00
v0.1.4 - 修复中途终止时上传成功但分享链接未打印的问题
This commit is contained in:
36
goecs.go
36
goecs.go
@@ -39,7 +39,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ecsVersion = "v0.1.3"
|
ecsVersion = "v0.1.4"
|
||||||
menuMode bool
|
menuMode bool
|
||||||
onlyChinaTest bool
|
onlyChinaTest bool
|
||||||
input, choice string
|
input, choice string
|
||||||
@@ -323,7 +323,6 @@ func main() {
|
|||||||
minutes := int(duration.Minutes())
|
minutes := int(duration.Minutes())
|
||||||
seconds := int(duration.Seconds()) % 60
|
seconds := int(duration.Seconds()) % 60
|
||||||
currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006")
|
currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006")
|
||||||
// 使用互斥锁保护output的写入
|
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
output = utils.PrintAndCapture(func() {
|
output = utils.PrintAndCapture(func() {
|
||||||
@@ -333,14 +332,32 @@ func main() {
|
|||||||
utils.PrintCenteredTitle("", width)
|
utils.PrintCenteredTitle("", width)
|
||||||
}, tempOutput, output)
|
}, tempOutput, output)
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
// 启动新的goroutine处理上传
|
// 创建一个通道来传递上传结果
|
||||||
|
resultChan := make(chan struct {
|
||||||
|
httpURL string
|
||||||
|
httpsURL string
|
||||||
|
}, 1) // 使用带缓冲的通道,避免可能的阻塞
|
||||||
|
// 启动上传
|
||||||
go func() {
|
go func() {
|
||||||
utils.ProcessAndUpload(output, filePath, enabelUpload)
|
httpURL, httpsURL := utils.ProcessAndUpload(output, filePath, enabelUpload)
|
||||||
|
resultChan <- struct {
|
||||||
|
httpURL string
|
||||||
|
httpsURL string
|
||||||
|
}{httpURL, httpsURL}
|
||||||
uploadDone <- true
|
uploadDone <- true
|
||||||
}()
|
}()
|
||||||
// 等待上传完成或超时
|
// 等待上传完成或超时
|
||||||
select {
|
select {
|
||||||
case <-uploadDone:
|
case result := <-resultChan:
|
||||||
|
if result.httpURL != "" || result.httpsURL != "" {
|
||||||
|
if language == "en" {
|
||||||
|
fmt.Printf("Upload successfully!\nHttp URL: %s\nHttps URL: %s\n", result.httpURL, result.httpsURL)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", result.httpURL, result.httpsURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 给打印操作一些时间完成
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
case <-time.After(30 * time.Second):
|
case <-time.After(30 * time.Second):
|
||||||
fmt.Println("上传超时,程序退出")
|
fmt.Println("上传超时,程序退出")
|
||||||
@@ -592,7 +609,14 @@ func main() {
|
|||||||
default:
|
default:
|
||||||
fmt.Println("Unsupported language")
|
fmt.Println("Unsupported language")
|
||||||
}
|
}
|
||||||
utils.ProcessAndUpload(output, filePath, enabelUpload)
|
httpURL, httpsURL := utils.ProcessAndUpload(output, filePath, enabelUpload)
|
||||||
|
if httpURL != "" || httpsURL != "" {
|
||||||
|
if language == "en" {
|
||||||
|
fmt.Printf("Upload successfully!\nHttp URL: %s\nHttps URL: %s\n", httpURL, httpsURL)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", httpURL, httpsURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
finish = true
|
finish = true
|
||||||
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
|
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
|
||||||
fmt.Println("Press Enter to exit...")
|
fmt.Println("Press Enter to exit...")
|
||||||
|
@@ -299,7 +299,7 @@ func UploadText(absPath string) (string, string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ProcessAndUpload 创建结果文件并上传文件
|
// ProcessAndUpload 创建结果文件并上传文件
|
||||||
func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
|
func ProcessAndUpload(output string, filePath string, enableUplaod bool) (string, string) {
|
||||||
// 使用 defer 来处理 panic
|
// 使用 defer 来处理 panic
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
@@ -312,14 +312,14 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
|
|||||||
err = os.Remove(filePath)
|
err = os.Remove(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("无法删除文件:", err)
|
fmt.Println("无法删除文件:", err)
|
||||||
return
|
return "", ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 创建文件
|
// 创建文件
|
||||||
file, err := os.Create(filePath)
|
file, err := os.Create(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("无法创建文件:", err)
|
fmt.Println("无法创建文件:", err)
|
||||||
return
|
return "", ""
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
// 匹配 ANSI 转义序列
|
// 匹配 ANSI 转义序列
|
||||||
@@ -331,13 +331,13 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
|
|||||||
_, err = writer.WriteString(cleanedOutput)
|
_, err = writer.WriteString(cleanedOutput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("无法写入文件:", err)
|
fmt.Println("无法写入文件:", err)
|
||||||
return
|
return "", ""
|
||||||
}
|
}
|
||||||
// 确保写入缓冲区的数据都刷新到文件中
|
// 确保写入缓冲区的数据都刷新到文件中
|
||||||
err = writer.Flush()
|
err = writer.Flush()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("无法刷新文件缓冲:", err)
|
fmt.Println("无法刷新文件缓冲:", err)
|
||||||
return
|
return "", ""
|
||||||
}
|
}
|
||||||
fmt.Printf("测试结果已写入 %s\n", filePath)
|
fmt.Printf("测试结果已写入 %s\n", filePath)
|
||||||
if enableUplaod {
|
if enableUplaod {
|
||||||
@@ -345,15 +345,16 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
|
|||||||
absPath, err := filepath.Abs(filePath)
|
absPath, err := filepath.Abs(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("无法获取文件绝对路径:", err)
|
fmt.Println("无法获取文件绝对路径:", err)
|
||||||
return
|
return "", ""
|
||||||
}
|
}
|
||||||
// 上传文件并生成短链接
|
// 上传文件并生成短链接
|
||||||
http_url, https_url, err := UploadText(absPath)
|
http_url, https_url, err := UploadText(absPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("上传失败,无法生成链接")
|
fmt.Println("上传失败,无法生成链接")
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return
|
return "", ""
|
||||||
}
|
}
|
||||||
fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", http_url, https_url)
|
return http_url, https_url
|
||||||
}
|
}
|
||||||
|
return "", ""
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user