mirror of
https://github.com/putyy/res-downloader.git
synced 2026-01-12 06:04:55 +08:00
优化拦截标识
This commit is contained in:
@@ -106,8 +106,6 @@ func (fd *FileDownloader) init() error {
|
||||
fd.IsMultiPart = true
|
||||
}
|
||||
|
||||
resp.Body.Close()
|
||||
|
||||
fd.FileName = filepath.Clean(fd.FileName)
|
||||
|
||||
dir := filepath.Dir(fd.FileName)
|
||||
|
||||
@@ -148,12 +148,12 @@ func (p *Proxy) handleWechatRequest(r *http.Request, ctx *goproxy.ProxyCtx) (*ht
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
resourceOnce.markMu.Lock()
|
||||
defer resourceOnce.markMu.Unlock()
|
||||
|
||||
urlSign := Md5(rowUrl.(string))
|
||||
if _, ok := resourceOnce.mark[urlSign]; ok {
|
||||
if resourceOnce.mediaIsMarked(urlSign) {
|
||||
return
|
||||
}
|
||||
|
||||
id, err := gonanoid.New()
|
||||
if err != nil {
|
||||
id = urlSign
|
||||
@@ -214,7 +214,7 @@ func (p *Proxy) handleWechatRequest(r *http.Request, ctx *goproxy.ProxyCtx) (*ht
|
||||
|
||||
res.OtherData["wx_file_formats"] = strings.Join(fileFormats, "#")
|
||||
}
|
||||
resourceOnce.mark[urlSign] = true
|
||||
resourceOnce.markMedia(urlSign)
|
||||
httpServerOnce.send("newResources", res)
|
||||
}(body)
|
||||
return r, p.buildEmptyResponse(r)
|
||||
@@ -310,14 +310,11 @@ func (p *Proxy) httpResponseEvent(resp *http.Response, ctx *goproxy.ProxyCtx) *h
|
||||
}
|
||||
|
||||
rawUrl := resp.Request.URL.String()
|
||||
resourceOnce.markMu.Lock()
|
||||
defer resourceOnce.markMu.Unlock()
|
||||
|
||||
isAll, _ := resourceOnce.getResType("all")
|
||||
isClassify, _ := resourceOnce.getResType(classify)
|
||||
|
||||
urlSign := Md5(rawUrl)
|
||||
if _, ok := resourceOnce.mark[urlSign]; !ok && (isAll || isClassify) {
|
||||
if ok := resourceOnce.mediaIsMarked(urlSign); !ok && (isAll || isClassify) {
|
||||
value, _ := strconv.ParseFloat(resp.Header.Get("content-length"), 64)
|
||||
id, err := gonanoid.New()
|
||||
if err != nil {
|
||||
@@ -339,7 +336,7 @@ func (p *Proxy) httpResponseEvent(resp *http.Response, ctx *goproxy.ProxyCtx) *h
|
||||
Description: "",
|
||||
ContentType: resp.Header.Get("Content-Type"),
|
||||
}
|
||||
resourceOnce.mark[urlSign] = true
|
||||
resourceOnce.markMedia(urlSign)
|
||||
httpServerOnce.send("newResources", res)
|
||||
}
|
||||
return resp
|
||||
|
||||
@@ -26,8 +26,7 @@ type WxFileDecodeResult struct {
|
||||
}
|
||||
|
||||
type Resource struct {
|
||||
mark map[string]bool
|
||||
markMu sync.RWMutex
|
||||
mediaMark sync.Map
|
||||
resType map[string]bool
|
||||
resTypeMu sync.RWMutex
|
||||
}
|
||||
@@ -35,7 +34,6 @@ type Resource struct {
|
||||
func initResource() *Resource {
|
||||
if resourceOnce == nil {
|
||||
resourceOnce = &Resource{
|
||||
mark: make(map[string]bool),
|
||||
resType: map[string]bool{
|
||||
"all": true,
|
||||
"image": true,
|
||||
@@ -52,17 +50,13 @@ func initResource() *Resource {
|
||||
return resourceOnce
|
||||
}
|
||||
|
||||
func (r *Resource) getMark(key string) (bool, bool) {
|
||||
r.markMu.RLock()
|
||||
defer r.markMu.RUnlock()
|
||||
value, ok := r.mark[key]
|
||||
return value, ok
|
||||
func (r *Resource) mediaIsMarked(key string) bool {
|
||||
_, loaded := r.mediaMark.Load(key)
|
||||
return loaded
|
||||
}
|
||||
|
||||
func (r *Resource) setMark(key string, value bool) {
|
||||
r.markMu.Lock()
|
||||
defer r.markMu.Unlock()
|
||||
r.mark[key] = value
|
||||
func (r *Resource) markMedia(key string) {
|
||||
r.mediaMark.Store(key, true)
|
||||
}
|
||||
|
||||
func (r *Resource) getResType(key string) (bool, bool) {
|
||||
@@ -93,15 +87,11 @@ func (r *Resource) setResType(n []string) {
|
||||
}
|
||||
|
||||
func (r *Resource) clear() {
|
||||
r.markMu.Lock()
|
||||
defer r.markMu.Unlock()
|
||||
r.mark = make(map[string]bool)
|
||||
r.mediaMark.Clear()
|
||||
}
|
||||
|
||||
func (r *Resource) delete(sign string) {
|
||||
r.markMu.Lock()
|
||||
defer r.markMu.Unlock()
|
||||
delete(r.mark, sign)
|
||||
r.mediaMark.Delete(sign)
|
||||
}
|
||||
|
||||
func (r *Resource) download(mediaInfo MediaInfo, decodeStr string) {
|
||||
|
||||
Reference in New Issue
Block a user