close
Attachments you submit will be routed for moderation. If you have an account, please log in first.

Ticket #43: patch_collide.diff

File patch_collide.diff, 3.1 KB (added by Lorenz Schwittmann, 9 years ago)
  • htmlgen.go

     
    1919
    2020import (
    2121        "bytes"
    22         "encoding/hex"
    2322        "errors"
    2423        "fmt"
    2524        "html"
     
    6261                        return fmt.Sprintf(`src="%v"`, url)
    6362                }
    6463                if options.inline {
    65                         return fmt.Sprintf(`data-imgsrc="res_%v"`, string(hex.EncodeToString([]byte(url))))
     64                        return fmt.Sprintf(`data-imgsrc="res_%v"`, urlToCopiedResourceName(url))
    6665                }
    6766                return fmt.Sprintf(`src="%v"`, url)
    6867        }
     
    157156
    158157func (this *HTMLGenerator) imgSrc(href string) string {
    159158        if this.options.inline && CanInline(href) {
    160                 return fmt.Sprintf(`data-imgsrc="res_%v"`, string(hex.EncodeToString([]byte(filepath.Base(href)))))
     159                return fmt.Sprintf(`data-imgsrc="res_%v"`, urlToCopiedResourceName(href))
    161160        }
    162161        if CanInline(href) { // The file will be copied
    163                 return fmt.Sprintf(`src="%v"`, filepath.Base(href))
     162                return fmt.Sprintf(`src="%v"`, urlToCopiedResourceNameExt(href))
    164163        }
    165164        return fmt.Sprintf(`src="%v"`, href)
    166165}
  • resource.go

     
    1919
    2020import (
    2121        "encoding/base64"
    22         "encoding/hex"
    2322        "fmt"
    2423        "io/ioutil"
    2524        "log"
     
    6463        return &Resource{name, true, resType, funcGetBytes}
    6564}
    6665
     66
     67func urlToCopiedResourceName(url string) string {
     68        return func_hash(url)
     69}
     70
     71func urlToCopiedResourceNameExt(url string) string {
     72        return urlToCopiedResourceName(url)+filepath.Ext(url)
     73}
     74
    6775func NewExternalResource(url string, resType resourceType) *Resource {
    6876        if CanInline(url) {
    6977                f := func() []byte {
     
    119127
    120128        switch this.resType {
    121129        case typeImage:
    122                 f.WriteString(fmt.Sprintf("window.res_%v = '%v';\n", string(hex.EncodeToString([]byte(filepath.Base(this.url)))), InlineImage(this.url, this.GetBytes())))
     130                f.WriteString(fmt.Sprintf("window.res_%v = '%v';\n", urlToCopiedResourceName(this.url), InlineImage(this.url, this.GetBytes())))
    123131        case typeCss, typeScript:
    124132                f.Write(this.GetBytes())
    125133        case typeFont:
     
    141149func (this *Resource) ToHtmlLink(f *os.File) {
    142150        url := this.url
    143151        // If the file will be copied and if it is not builtin, remove the path from the URL
    144         if !this.builtin && !strings.HasPrefix(this.url, "http:") && !strings.HasPrefix(this.url, "https:") {
    145                 url = filepath.Base(url)
     152        if !strings.HasPrefix(this.url, "http:") && !strings.HasPrefix(this.url, "https:") {
     153                url = urlToCopiedResourceNameExt(url)
    146154        }
    147155        switch this.resType {
    148156        case typeCss:
     
    171179        }
    172180
    173181        if this.builtin {
    174                 err := ioutil.WriteFile(filepath.Join(destpath, this.url), this.GetBytes(), 0664)
     182                err := ioutil.WriteFile(filepath.Join(destpath, urlToCopiedResourceNameExt(this.url)), this.GetBytes(), 0664)
    175183                if err != nil {
    176184                        log.Printf("Could not write file %v", this.url)
    177185                }
    178186        } else {
    179                 CopyFile(filepath.Join(destpath, filepath.Base(this.url)), this.url)
     187                CopyFile(filepath.Join(destpath, urlToCopiedResourceNameExt(this.url)), this.url)
    180188        }
    181189
    182190        writtenResource[this.url] = true