Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. /*
  2. Copyright (c) 2019, Saleem Mirza
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the <organization> nor the
  13. names of its contributors may be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15.  
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27.  
  28. package main
  29.  
  30. import (
  31. "flag"
  32. "fmt"
  33. "io/ioutil"
  34. "log"
  35. "os"
  36. "path"
  37. "path/filepath"
  38. "regexp"
  39. "strings"
  40. "time"
  41. )
  42.  
  43. var (
  44. homeDirectory string
  45. workDirectory string
  46. filter string
  47. rxString = `(?msi)<script[^>]+?src=\"~?\/(.+?)(\?.+?)?\".*?<\/script>`
  48. rx = regexp.MustCompile(rxString)
  49. scriptInfoMap = make(map[string]int64)
  50. extensionsMap = make(map[string]bool)
  51. )
  52.  
  53. func init() {
  54. currentDirectory, _ := os.Getwd()
  55. flag.StringVar(&homeDirectory, "homeDirectory", currentDirectory, "set home directory")
  56. flag.StringVar(&workDirectory, "workDirectory", currentDirectory, "set working directory")
  57. flag.StringVar(&filter, "filter", ".html", "file types to apply changes.")
  58. }
  59.  
  60. func main() {
  61. var logoText = `
  62. oo oo oo oo
  63. oo oo oo oo ooo
  64. ooooo ooooooo
  65. ooooo ooooooo
  66. ooooo ooooo
  67. oooo ooo
  68. oo ooo
  69. o oooo
  70. oo ooo oo ooooooo
  71. o oooo ooooo oooooo o
  72. o ooooo oooooooo oooooo o
  73. oo ooooo ooooooo ooooooo o
  74. o ooo oooo ooooooooo o
  75. oo oooooooooo ooo
  76. ooo oooooo ooooooooooo oooo
  77. ooooo oooooo ooooooooooooo oooooo
  78. ooooooo ooooooooooooooo oooooooo
  79. oooooooo ooo ooooooooooooooo ooooooooo
  80. oooooooooooo oooooooooooooooooooooooooo
  81. ooooooooooooooooooooooooooooo oooooooooo
  82. ooooooooo ooooooo ooooooooooo oooooooo
  83. ooooooooooo ooooooooooo ooooo
  84. oooooooooo ooooooooooo
  85. ooooooo oooooooooooo
  86. ooooooooooo
  87. oooooooooo
  88. `
  89.  
  90. fmt.Println(logoText)
  91. flag.Usage = func() {
  92. fmt.Println("\nUsage:")
  93.  
  94. flag.PrintDefaults()
  95. fmt.Println()
  96. }
  97. flag.Parse()
  98.  
  99. for _, v := range strings.Split(filter, ";") {
  100. extensionsMap[v] = true
  101. }
  102.  
  103. fileCounter := 0
  104. var startTime = time.Now()
  105.  
  106. err := filepath.Walk(workDirectory, func(path string, info os.FileInfo, err error) error {
  107. if err != nil {
  108. return err
  109. }
  110.  
  111. if info.IsDir() {
  112. return nil
  113. }
  114.  
  115. fileExt := filepath.Ext(info.Name())
  116. if _, ok := extensionsMap[fileExt]; ok == true {
  117. fileCounter++
  118. _ = processFile(path)
  119. }
  120.  
  121. return nil
  122. })
  123.  
  124. fmt.Printf("\n\n%d files processed in %0.3f seconds\n", fileCounter, time.Since(startTime).Seconds())
  125.  
  126. if err != nil {
  127. println(err)
  128. }
  129. }
  130.  
  131. func processFile(filePath string) error {
  132. data, e := ioutil.ReadFile(filePath)
  133. if e == nil {
  134. fileContents := string(data)
  135.  
  136. matchIndices := rx.FindAllStringSubmatchIndex(fileContents, -1)
  137. if matchIndices != nil {
  138. file, err := os.OpenFile(filePath, os.O_TRUNC|os.O_WRONLY, 0777)
  139. if err != nil {
  140. log.Println(err)
  141. return err
  142. }
  143.  
  144. defer file.Close()
  145. _ = file.Truncate(0)
  146.  
  147. lastIndex := 0
  148. for _, v := range matchIndices {
  149. _, _ = file.WriteString(fileContents[lastIndex:v[2]])
  150.  
  151. script := fileContents[v[2]:v[3]]
  152. scriptPath := path.Join(homeDirectory, script)
  153.  
  154. fileInfo, err := os.Stat(scriptPath)
  155. if err == nil {
  156. _, _ = file.WriteString(fmt.Sprintf("%s?v=%v", script, fileInfo.ModTime().Unix()))
  157. } else {
  158. log.Printf("[ NOT FOUND: %s ] %s\n", script, filePath)
  159. _, _ = file.WriteString(script)
  160. }
  161.  
  162. if v[5] < 1 {
  163. lastIndex = v[3]
  164. } else {
  165. lastIndex = v[5]
  166. }
  167.  
  168. }
  169. _, _ = file.WriteString(fileContents[lastIndex:])
  170. }
  171. return nil
  172. } else {
  173. return e
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement