提交 dedb7743 作者: 史连宁

压缩图片路径转换

上级 c14fa2f0
...@@ -38,37 +38,25 @@ func LocalImageFileCheck(provId, beginDate, endDate, fileDir string, threadNum i ...@@ -38,37 +38,25 @@ func LocalImageFileCheck(provId, beginDate, endDate, fileDir string, threadNum i
return "", "", 0 return "", "", 0
} }
needDownChan := make(chan datImage, allCount) var needDownList []datImage
bar := progressbar.Default(allCount) fmt.Printf("核验数据量:%d\n", allCount)
go func() { for _, task := range datImageList {
wg := sync.WaitGroup{} chackExistFile(&needDownList, task, fileDir)
wg.Add(len(datImageList)) }
p, _ := ants.NewPool(threadNum) bar := progressbar.Default(int64(len(needDownList)))
defer p.Release()
fmt.Printf("核验数据量:%d,协程池容量:%d\n", allCount, threadNum)
for _, task := range datImageList {
_ = p.Submit(func() {
defer wg.Done()
chackExistFile(needDownChan, task, fileDir)
})
}
wg.Wait()
close(needDownChan)
bar.ChangeMax(len(needDownChan))
}()
noImageChan := make(chan []interface{}, allCount) noImageChan := make(chan []interface{}, allCount)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(needDownList))
pd, _ := ants.NewPool(threadNumNet) pd, _ := ants.NewPool(threadNumNet)
defer pd.Release() defer pd.Release()
fmt.Printf("下载协程池容量:%d\n", threadNumNet) fmt.Printf("下载协程池容量:%d\n", threadNumNet)
for task := range needDownChan { for _, task := range needDownList {
wg.Add(1) t := task
_ = pd.Submit(func() { _ = pd.Submit(func() {
defer wg.Done() defer wg.Done()
dwonloadOne(noImageChan, task, fileDir) dwonloadOne(noImageChan, t, fileDir)
bar.Add(1) bar.Add(1)
}) })
} }
...@@ -79,7 +67,7 @@ func LocalImageFileCheck(provId, beginDate, endDate, fileDir string, threadNum i ...@@ -79,7 +67,7 @@ func LocalImageFileCheck(provId, beginDate, endDate, fileDir string, threadNum i
return f, m, len(datImageList) return f, m, len(datImageList)
} }
func chackExistFile(needDownChan chan datImage, task datImage, fileDir string) { func chackExistFile(needDownList *[]datImage, task datImage, fileDir string) {
originalPath := fmt.Sprintf("%s/%s", fileDir, strings.ReplaceAll(task.ImageCode, "?pa=Y", "")) originalPath := fmt.Sprintf("%s/%s", fileDir, strings.ReplaceAll(task.ImageCode, "?pa=Y", ""))
pressedPath := strings.ReplaceAll(originalPath, "original", "pressed") pressedPath := strings.ReplaceAll(originalPath, "original", "pressed")
task.noOriginal = false task.noOriginal = false
...@@ -91,7 +79,7 @@ func chackExistFile(needDownChan chan datImage, task datImage, fileDir string) { ...@@ -91,7 +79,7 @@ func chackExistFile(needDownChan chan datImage, task datImage, fileDir string) {
task.noPressed = true task.noPressed = true
} }
if task.noOriginal || task.noPressed { if task.noOriginal || task.noPressed {
needDownChan <- task *needDownList = append(*needDownList, task)
} }
} }
...@@ -99,13 +87,13 @@ func dwonloadOne(noImageChan chan []interface{}, task datImage, fileDir string) ...@@ -99,13 +87,13 @@ func dwonloadOne(noImageChan chan []interface{}, task datImage, fileDir string)
originalPath := fmt.Sprintf("%s/%s", fileDir, strings.ReplaceAll(task.ImageCode, "?pa=Y", "")) originalPath := fmt.Sprintf("%s/%s", fileDir, strings.ReplaceAll(task.ImageCode, "?pa=Y", ""))
pressedPath := strings.ReplaceAll(originalPath, "original", "pressed") pressedPath := strings.ReplaceAll(originalPath, "original", "pressed")
if task.noOriginal { if task.noOriginal {
task.noOriginal = downOriginalFile(originalPath, task) task.noOriginal = !downOriginalFile(originalPath, task)
} }
if task.noPressed { if task.noPressed {
task.noPressed = downPressedFile(pressedPath, task, task.noOriginal, originalPath) task.noPressed = !downPressedFile(pressedPath, task, task.noOriginal, originalPath)
} }
if task.noOriginal || task.noPressed { if task.noOriginal || task.noPressed {
noImageChan <- []interface{}{task.ImageID, task.noOriginal, task.noPressed, task.ImageCode, task.OriginUrl.String} noImageChan <- []interface{}{task.ImageID, !task.noOriginal, !task.noPressed, task.ImageCode, task.OriginUrl.String}
} }
} }
...@@ -166,11 +154,14 @@ func downOriginalFile(savePath string, di datImage) bool { ...@@ -166,11 +154,14 @@ func downOriginalFile(savePath string, di datImage) bool {
} }
func downPressedFile(savePath string, di datImage, noOriginal bool, originalPath string) bool { func downPressedFile(savePath string, di datImage, noOriginal bool, originalPath string) bool {
filePath := strings.ReplaceAll(di.ImageCode, "original", "pressed")
fileNameIdx := strings.LastIndex(filePath, "/") + 1
fileName := filePath[fileNameIdx:]
err := downRemoteFile( err := downRemoteFile(
savePath, savePath,
fmt.Sprintf( fmt.Sprintf(
"https://iotimage.pestiot.com/iotImage/%s", "https://iotimage.pestiot.com/iotImage/%s",
strings.ReplaceAll(di.ImageCode, "original", "pressed"))) strings.ReplaceAll(filePath, fileName, "p"+fileName)))
if err == nil { if err == nil {
return true return true
} }
...@@ -195,11 +186,20 @@ func downRemoteFile(savePath, url string) error { ...@@ -195,11 +186,20 @@ func downRemoteFile(savePath, url string) error {
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("[DownloadError][%d]%s", response.StatusCode, response.String())) return errors.New(fmt.Sprintf("[DownloadError][%d]%s", response.StatusCode, response.String()))
} }
spIdx := strings.LastIndexByte(savePath, '/')
err = os.MkdirAll(savePath[:spIdx], os.ModeDir)
if err != nil {
return err
}
err = response.DownloadToFile(savePath) err = response.DownloadToFile(savePath)
return err if err != nil {
return err
}
return nil
} }
func genNoImageDataReport(noImageChan chan []interface{}, taskName string) (filepath, msg string) { func genNoImageDataReport(noImageChan chan []interface{}, taskName string) (filepath, msg string) {
fmt.Printf("生成报告,%d条", len(noImageChan))
if len(noImageChan) > 0 { if len(noImageChan) > 0 {
xlsx := excelize.NewFile() xlsx := excelize.NewFile()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论