main.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "gdyb/models"
  6. _ "gdyb/routers"
  7. "gdyb/service"
  8. "github.com/astaxie/beego"
  9. "github.com/qiniu/api.v7/auth/qbox"
  10. "github.com/qiniu/api.v7/storage"
  11. "os"
  12. "strconv"
  13. "time"
  14. )
  15. func init() {
  16. service.ConnectDB()
  17. //org_id, _ := beego.AppConfig.Int64("org_id")
  18. //miConfig, _ := service.FindMedicalInsuranceInfo(org_id)
  19. //CreateLog(miConfig)
  20. //UploadLog(miConfig)
  21. }
  22. func main() {
  23. beego.Run()
  24. }
  25. func CreateLog(miConfig models.MedicalInsuranceOrgConfig) {
  26. Mkdir(miConfig.OrgName + "日志")
  27. //org_id,_ := beego.AppConfig.Int("org_id")
  28. month := time.Unix(1557042972, 0).Format("1")
  29. year := time.Now().Format("2006")
  30. month = time.Now().Format("01")
  31. day := time.Now().Format("02")
  32. file := strconv.FormatInt(miConfig.UserOrgId, 10) + "_" + year + month + day + "_log"
  33. file_name := file + ".txt"
  34. file_path := miConfig.OrgName + "日志" + "/" + file_name
  35. exist, _ := PathExists(file_path)
  36. if exist { //存在
  37. fmt.Println("存在")
  38. f, err := os.OpenFile(file_path, os.O_WRONLY, 0644)
  39. if err != nil {
  40. fmt.Println("read fail")
  41. }
  42. content := "\r\r\r"
  43. n, _ := f.Seek(0, 2)
  44. _, err = f.WriteAt([]byte(content), n)
  45. } else { //不存在
  46. fmt.Println("文件不存在,创建文件")
  47. f, err := os.Create(miConfig.OrgName + "日志" + "/" + file_name)
  48. defer f.Close()
  49. if err != nil {
  50. } else {
  51. _, err = f.Write([]byte("记录日志"))
  52. }
  53. }
  54. }
  55. func UploadLog(miConfig models.MedicalInsuranceOrgConfig) {
  56. //获取七牛云上的文件信息,不存在则表示没上传到七牛云,上传昨天日志信息
  57. nTime := time.Now()
  58. yesTime := nTime.AddDate(0, 0, -1)
  59. file_key := strconv.FormatInt(miConfig.UserOrgId, 10) + "_" + yesTime.Format("20060102") + "_log.txt"
  60. mac := qbox.NewMac(beego.AppConfig.String("qiniu_accesskey"), beego.AppConfig.String("qiniu_secretkey"))
  61. cfg := storage.Config{
  62. // 是否使用https域名进行资源管理
  63. UseHTTPS: true,
  64. }
  65. // 指定空间所在的区域,如果不指定将自动探测
  66. // 如果没有特殊需求,默认不需要指定
  67. //cfg.Zone=&storage.ZoneHuabei
  68. bucketManager := storage.NewBucketManager(mac, &cfg)
  69. bucket := beego.AppConfig.String("qiniu_bucket")
  70. key := file_key
  71. file, sErr := bucketManager.Stat(bucket, key)
  72. fmt.Println(file)
  73. fmt.Println(sErr)
  74. fmt.Println(miConfig.OrgName + "日志" + "/" + file_key)
  75. if sErr != nil {
  76. fmt.Println("七牛云上文件不存在,上传到七牛云")
  77. //判断本地文件是否存在,存在则执行上传操作
  78. exist, _ := PathExists(miConfig.OrgName + "日志" + "/" + file_key)
  79. if exist {
  80. upload_qiniu(miConfig.OrgName+"日志"+"/"+file_key, file_key)
  81. } else {
  82. fmt.Println("本地文件不存在")
  83. }
  84. } else {
  85. fmt.Println("七牛云文件存在")
  86. }
  87. }
  88. func Mkdir(dir string) {
  89. // 创建文件夹
  90. exist, err := PathExists(dir)
  91. if err != nil {
  92. fmt.Println(err.Error())
  93. } else {
  94. if exist {
  95. fmt.Println(dir + "文件夹已存在!")
  96. } else {
  97. // 文件夹名称,权限
  98. err := os.Mkdir(dir, os.ModePerm)
  99. if err != nil {
  100. fmt.Println(dir+"文件夹创建失败:", err.Error())
  101. } else {
  102. fmt.Println(dir + "文件夹创建成功!")
  103. }
  104. }
  105. }
  106. }
  107. func PathExists(path string) (bool, error) {
  108. _, err := os.Stat(path)
  109. if err == nil {
  110. return true, nil
  111. }
  112. if os.IsNotExist(err) {
  113. return false, nil
  114. }
  115. return false, err
  116. }
  117. func upload_qiniu(filePath string, keys string) {
  118. key := keys
  119. //上传凭证,关于凭证这块大家可以去看看官方文档
  120. putPolicy := storage.PutPolicy{
  121. Scope: beego.AppConfig.String("qiniu_bucket"),
  122. }
  123. mac := qbox.NewMac(beego.AppConfig.String("qiniu_accesskey"), beego.AppConfig.String("qiniu_secretkey"))
  124. upToken := putPolicy.UploadToken(mac)
  125. cfg := storage.Config{}
  126. //空间对应机房
  127. //其中关于Zone对象和机房的关系如下:
  128. // 机房 Zone对象
  129. // 华东 storage.ZoneHuadong
  130. // 华北 storage.ZoneHuabei
  131. // 华南 storage.ZoneHuanan
  132. // 北美 storage.ZoneBeimei
  133. //七牛云存储空间设置首页有存储区域
  134. cfg.Zone = &storage.ZoneHuadong
  135. //不启用HTTPS域名
  136. cfg.UseHTTPS = false
  137. //不使用CND加速
  138. cfg.UseCdnDomains = false
  139. //构建上传表单对象
  140. formUploader := storage.NewFormUploader(&cfg)
  141. ret := storage.PutRet{}
  142. // 可选
  143. putExtra := storage.PutExtra{
  144. Params: map[string]string{},
  145. }
  146. err := formUploader.PutFile(context.Background(), &ret, upToken, key, filePath, &putExtra)
  147. if err != nil {
  148. fmt.Println(err)
  149. return
  150. }
  151. fmt.Println(ret.Key, ret.Hash)
  152. }