main.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package main
  2. import (
  3. "archive/zip"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "reflect"
  14. "strings"
  15. "time"
  16. )
  17. func in_array(val interface{}, array interface{}) (exists bool, index int) {
  18. exists = false
  19. index = -1
  20. switch reflect.TypeOf(array).Kind() {
  21. case reflect.Slice:
  22. s := reflect.ValueOf(array)
  23. for i := 0; i < s.Len(); i++ {
  24. if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
  25. index = i
  26. exists = true
  27. return
  28. }
  29. }
  30. }
  31. return
  32. }
  33. func file_exists(path string) bool {
  34. if _, err := os.Stat(path); os.IsNotExist(err) {
  35. return false
  36. }
  37. return true
  38. }
  39. func file_get_contents(path string) string {
  40. dat, err := ioutil.ReadFile(path)
  41. if err != nil {
  42. panic("Unable to read file: " + path)
  43. }
  44. return (string(dat))
  45. }
  46. func writeLog(filepath string, prefix string, message string) bool {
  47. f, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  48. if err != nil {
  49. log.Println(err)
  50. }
  51. defer f.Close()
  52. logger := log.New(f, prefix, log.LstdFlags)
  53. logger.Println(message)
  54. return true
  55. }
  56. func compress(source, target string) error {
  57. zipfile, err := os.Create(target)
  58. if err != nil {
  59. return err
  60. }
  61. defer zipfile.Close()
  62. archive := zip.NewWriter(zipfile)
  63. defer archive.Close()
  64. info, err := os.Stat(source)
  65. if err != nil {
  66. return nil
  67. }
  68. var baseDir string
  69. if info.IsDir() {
  70. baseDir = filepath.Base(source)
  71. }
  72. filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
  73. if err != nil {
  74. return err
  75. }
  76. header, err := zip.FileInfoHeader(info)
  77. if err != nil {
  78. return err
  79. }
  80. if baseDir != "" {
  81. header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
  82. }
  83. if info.IsDir() {
  84. header.Name += "/"
  85. } else {
  86. header.Method = zip.Deflate
  87. }
  88. writer, err := archive.CreateHeader(header)
  89. if err != nil {
  90. return err
  91. }
  92. if info.IsDir() {
  93. return nil
  94. }
  95. file, err := os.Open(path)
  96. if err != nil {
  97. return err
  98. }
  99. defer file.Close()
  100. _, err = io.Copy(writer, file)
  101. return err
  102. })
  103. return err
  104. }
  105. func unzip(archive, target string) error {
  106. reader, err := zip.OpenReader(archive)
  107. if err != nil {
  108. return err
  109. }
  110. if err := os.MkdirAll(target, 0755); err != nil {
  111. return err
  112. }
  113. for _, file := range reader.File {
  114. path := filepath.Join(target, file.Name)
  115. if file.FileInfo().IsDir() {
  116. os.MkdirAll(path, file.Mode())
  117. continue
  118. }
  119. fileReader, err := file.Open()
  120. if err != nil {
  121. return err
  122. }
  123. defer fileReader.Close()
  124. targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
  125. if err != nil {
  126. return err
  127. }
  128. defer targetFile.Close()
  129. if _, err := io.Copy(targetFile, fileReader); err != nil {
  130. return err
  131. }
  132. }
  133. return nil
  134. }
  135. func getTimestamp() string {
  136. t := time.Now()
  137. return (t.Format("2006-01-02 150405"))
  138. }
  139. func decodeBase64(base64string string) string {
  140. l, err := base64.StdEncoding.DecodeString(base64string)
  141. if err != nil {
  142. panic(err)
  143. }
  144. return string(l)
  145. }
  146. func moveLogFile(logfile string, target string, uuid string) {
  147. //If then else is used to prevent invalid move target
  148. if target == "done" {
  149. os.Rename(logfile, "log/done/"+uuid+".log")
  150. os.Chmod("log/done/"+uuid+".log", 0777) //Unlock the file for read write from php
  151. } else if target == "error" {
  152. os.Rename(logfile, "log/error/"+uuid+".log")
  153. os.Chmod("log/error/"+uuid+".log", 0777) //Unlock the file for read write from php
  154. } else {
  155. //What happened?
  156. panic("ERROR. Undefined log file sattle location.")
  157. }
  158. }
  159. func finishFileOperation(logfile string, uuid string) {
  160. //Finishing the file operation.
  161. //time.Sleep(20000 * time.Millisecond) //Added this line for debug purpose
  162. writeLog(logfile, "[done] ", "Task finished successfully")
  163. moveLogFile(logfile, "done", uuid)
  164. fmt.Println("Done")
  165. }
  166. func isDir(path string) bool {
  167. file, _ := os.Open(path)
  168. if fi, err := file.Stat(); err != nil || fi.IsDir() {
  169. return true
  170. }
  171. file.Close()
  172. return false
  173. }
  174. func raiseError(logfile string, uuid string, errMsg string) {
  175. writeLog(logfile, "[error] ", errMsg)
  176. moveLogFile(logfile, "error", uuid)
  177. panic("ERROR. " + errMsg)
  178. }
  179. func ChmodR(path string) error {
  180. return filepath.Walk(path, func(name string, info os.FileInfo, err error) error {
  181. if err == nil {
  182. err = os.Chmod(name, 0755)
  183. }
  184. return err
  185. })
  186. }
  187. func main() {
  188. //Init the log folder if it doesn't exists
  189. if !file_exists("log/") {
  190. os.MkdirAll("log/", 0777)
  191. }
  192. if !file_exists("log/done/") {
  193. os.MkdirAll("log/done/", 0777)
  194. }
  195. if !file_exists("log/error/") {
  196. os.MkdirAll("log/error/", 0777)
  197. }
  198. //Check argument if satisfied
  199. if len(os.Args) < 3 {
  200. fmt.Println("<<ArOZ Online System File System Compress File Creator>>")
  201. fmt.Println("Usage: fszip {uuid} {command_in_base64_JSON}")
  202. fmt.Println("Command must be an array containing [{'zip' / 'unzip'},{filepath},{zip_target}]")
  203. return
  204. }
  205. startSettings := os.Args[1:]
  206. uuid := startSettings[0]
  207. encodedCommand := startSettings[1]
  208. decodedCommand := decodeBase64(encodedCommand)
  209. filename := uuid + ".log"
  210. logfile := "log/" + filename
  211. var command []string
  212. //Check if the given uuid already exists. If yes, terminate the execution
  213. if file_exists("log/"+uuid+".log") || file_exists("log/done/"+uuid+".log") || file_exists("log/error/"+uuid+".log") {
  214. panic("ERROR. Given uuid already exists.")
  215. }
  216. //Create the log file for this task
  217. writeLog(logfile, "[init] ", "Task created on "+getTimestamp())
  218. //Parse the JSON string into readable array
  219. err := json.Unmarshal([]byte(decodedCommand), &command)
  220. if err != nil {
  221. writeLog(logfile, "[error] ", "Unable to parse JSON string.")
  222. moveLogFile(logfile, "error", uuid)
  223. panic("ERROR. Unable to parse JSON string.")
  224. }
  225. //Check opr
  226. if command[0] == "zip" {
  227. //Start the compression process for the given path
  228. //Command structure: ["zip","{source}","{target} (Pass in empty string for default location)"]
  229. //Check if the given filepath exists
  230. if !file_exists(command[1]) {
  231. raiseError(logfile, uuid, "Source not exists.")
  232. }
  233. inpath := command[1]
  234. //Check if inpath end with "/". If yes, remove it
  235. if inpath[len(inpath)-1:] == "/" {
  236. inpath = inpath[0 : len(inpath)-1]
  237. }
  238. outpath := command[2]
  239. //File exists. Check if it is a folder or not.
  240. if outpath != "" {
  241. //If there is a defined export filepath, check if the parent dir exists. If not, create it
  242. if !file_exists(path.Dir(outpath)) {
  243. os.MkdirAll(path.Dir(outpath), 0755)
  244. }
  245. } else {
  246. //There is no defined export path. Export next to the source as zip.
  247. if isDir(command[1]) {
  248. outpath = path.Dir(path.Dir(inpath)) + "/" + path.Base(inpath) + ".zip"
  249. } else {
  250. outpath = path.Dir(inpath) + "/" + strings.TrimSuffix(inpath, filepath.Ext(inpath)) + ".zip"
  251. }
  252. }
  253. if file_exists(outpath) {
  254. raiseError(logfile, uuid, "Output file already exists.")
  255. }
  256. compress(inpath, outpath)
  257. os.Chmod(outpath, 0777)
  258. finishFileOperation(logfile, uuid)
  259. } else if command[0] == "unzip" {
  260. //Unzip a given filepath
  261. //command structure: ["unzip","{source}","{target}"]
  262. if !file_exists(command[1]) {
  263. raiseError(logfile, uuid, "Source not exists.")
  264. }
  265. inpath := command[1]
  266. outpath := command[2]
  267. if outpath == "" {
  268. //If unzup target is empty string, unzip to the current directory which the script is executed
  269. outpath = "./"
  270. }
  271. //Check if the input path is a zip file. If not, post a warning into the log file
  272. if filepath.Ext(inpath) != ".zip" {
  273. writeLog(logfile, "[warning] ", "The given file might not be a zip file. Trying to open it anyway. File ext given: "+filepath.Ext(inpath))
  274. }
  275. //Create outpath if it doesn't exists
  276. if !file_exists(outpath) {
  277. os.MkdirAll(outpath, 0755)
  278. }
  279. unzip(inpath, outpath)
  280. if isDir(outpath) {
  281. ChmodR(outpath)
  282. } else {
  283. os.Chmod(outpath, 0755)
  284. }
  285. finishFileOperation(logfile, uuid)
  286. }
  287. }