package.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package service
  2. import (
  3. "fmt"
  4. "go-common/app/admin/main/macross/conf"
  5. "go-common/app/admin/main/macross/model/package"
  6. "go-common/app/admin/main/macross/tools"
  7. "go-common/library/log"
  8. "io"
  9. "mime/multipart"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. )
  14. // PackageUpload upload package zip file & unzip it
  15. func (s *Service) PackageUpload(file multipart.File, pkgInfo upload.PkgInfo) (err error) {
  16. err = os.MkdirAll(pkgInfo.SaveDir, 0755)
  17. if err != nil {
  18. log.Error("os.MkdirAll error(%v)", err)
  19. return
  20. }
  21. destFilePath := filepath.Join(pkgInfo.SaveDir, pkgInfo.FileName)
  22. destFile, err := os.Create(destFilePath)
  23. if err != nil {
  24. log.Error("os.Create(%s) error(%v)", destFilePath, err)
  25. return
  26. }
  27. io.Copy(destFile, file)
  28. err = tools.Unzip(destFilePath, pkgInfo.SaveDir)
  29. if err != nil {
  30. log.Error("unzip(%s, %s) error(%v)", destFilePath, pkgInfo.SaveDir, err)
  31. return
  32. }
  33. err = os.Remove(destFilePath)
  34. if err != nil {
  35. log.Error("os.Remove(%s) error(%v)", destFilePath, err)
  36. return
  37. }
  38. // generate Android channel package
  39. if pkgInfo.ClientType == "android" {
  40. if len(pkgInfo.Channel) > 0 && len(pkgInfo.ApkName) > 0 {
  41. dest := filepath.Join(pkgInfo.SaveDir, "channel")
  42. targetFilePath := filepath.Join(pkgInfo.SaveDir, pkgInfo.ApkName)
  43. _, err = tools.GenerateChannelApk(dest, pkgInfo.Channel, nil, targetFilePath, false, false)
  44. if err != nil {
  45. log.Error("tools.GenerateChannelApk(%s, %s, nil, %s, false, false) error(%v)", dest, pkgInfo.Channel, targetFilePath, err)
  46. os.RemoveAll(pkgInfo.SaveDir)
  47. return
  48. }
  49. }
  50. }
  51. return
  52. }
  53. // PackageList list the Package Files
  54. func (s *Service) PackageList(path string) (fileList []string, err error) {
  55. err = filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
  56. if err != nil {
  57. log.Error("filepath.Walk error(%v)", err)
  58. return err
  59. }
  60. if f == nil {
  61. errMsg := "found no file"
  62. err = fmt.Errorf(errMsg)
  63. log.Error(errMsg)
  64. return err
  65. }
  66. if f.IsDir() {
  67. return nil
  68. }
  69. fileURL := strings.Replace(path, conf.Conf.Property.Package.SavePath, conf.Conf.Property.Package.URLPrefix, -1)
  70. fileList = append(fileList, fileURL)
  71. return err
  72. })
  73. return
  74. }