upload.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. package api
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "github.com/axetroy/go-fs"
  8. "github.com/gin-gonic/gin"
  9. "github.com/kptyun/KPTCOMM/pkg/app"
  10. "github.com/kptyun/KPTCOMM/pkg/e"
  11. "github.com/kptyun/KPTCOMM/pkg/logging"
  12. "github.com/kptyun/KPTCOMM/pkg/setting"
  13. "github.com/kptyun/KPTCOMM/routers/restful"
  14. "github.com/nfnt/resize"
  15. "image"
  16. "image/gif"
  17. "image/jpeg"
  18. "image/png"
  19. "io"
  20. "mime/multipart"
  21. "net/http"
  22. "os"
  23. "path"
  24. "strconv"
  25. "strings"
  26. )
  27. // 支持的图片后缀名
  28. var supportImageExtNames = []string{".jpg", ".jpeg", ".png", ".ico", ".svg", ".bmp", ".gif"}
  29. /*
  30. *
  31. check a file is a image or not
  32. */
  33. func isImage(extName string) bool {
  34. for i := 0; i < len(supportImageExtNames); i++ {
  35. if supportImageExtNames[i] == extName {
  36. return true
  37. }
  38. }
  39. return false
  40. }
  41. /*
  42. *
  43. Handler the parse error
  44. */
  45. func parseFormFail(context *gin.Context) {
  46. context.JSON(http.StatusBadRequest, gin.H{
  47. "message": "Can not parse form",
  48. })
  49. }
  50. /*
  51. *
  52. Upload file handler
  53. */
  54. func UploadFile(context *gin.Context) {
  55. appG := app.Gin{C: context}
  56. dictId := context.Param("id")
  57. dictName := context.Param("name")
  58. logging.Info("UploadFile ", context.Keys, dictId, dictName)
  59. var (
  60. isSupportFile bool
  61. maxUploadSize = setting.AppSetting.FileMaxSize // 最大上传大小
  62. allowTypes = setting.AppSetting.FileAllowType // 可上传的文件类型
  63. distPath string // 最终的输出目录
  64. err error
  65. file *multipart.FileHeader
  66. src multipart.File
  67. dist *os.File
  68. )
  69. // Source
  70. if file, err = context.FormFile("file"); err != nil {
  71. parseFormFail(context)
  72. return
  73. }
  74. sqlname := context.GetHeader("optname")
  75. extname := path.Ext(file.Filename)
  76. if len(allowTypes) != 0 {
  77. for i := 0; i < len(allowTypes); i++ {
  78. if allowTypes[i] == extname {
  79. isSupportFile = true
  80. break
  81. }
  82. }
  83. if isSupportFile == false {
  84. context.JSON(http.StatusBadRequest, gin.H{
  85. "message": "不支持的文件类型: " + extname,
  86. })
  87. return
  88. }
  89. }
  90. if file.Size > int64(maxUploadSize) {
  91. context.JSON(http.StatusBadRequest, gin.H{
  92. "message": "上传文件太大, 最大限制为(byte): " + strconv.Itoa(int(maxUploadSize)),
  93. })
  94. return
  95. }
  96. if src, err = file.Open(); err != nil {
  97. // open the file fail...
  98. }
  99. defer src.Close()
  100. hash := md5.New()
  101. io.Copy(hash, src)
  102. md5string := hex.EncodeToString(hash.Sum([]byte("")))
  103. fileName := md5string + extname
  104. // Destination
  105. filePath := setting.CurrentPath + setting.AppSetting.FileSavePath + dictName + "/"
  106. err = PathCheck(filePath) //检查路径并创建
  107. if err != nil {
  108. fmt.Println("PathCheck err", err)
  109. }
  110. distPath = path.Join(filePath, fileName)
  111. if dist, err = os.Create(distPath); err != nil {
  112. fmt.Println("Create err", err)
  113. }
  114. defer dist.Close()
  115. //distPath = setting.CurrentPath + setting.AppSetting.FileSavePath +"/"+ fileName
  116. if dist, err = os.Create(distPath); err != nil {
  117. // create dist file fail...
  118. }
  119. defer dist.Close()
  120. // FIXME: open 2 times
  121. if src, err = file.Open(); err != nil {
  122. //
  123. }
  124. // Copy
  125. io.Copy(dist, src)
  126. var execresult interface{}
  127. params := context.Request.Form
  128. if sqlname != "" {
  129. sql, p := restful.GetSqlByNameDB(sqlname) //todo insertcustomdoc
  130. if sql != "" {
  131. s_params := make([]interface{}, 0)
  132. paramslist := strings.Split(p, ",")
  133. if len(paramslist) > 0 && p != "" {
  134. for _, value := range paramslist {
  135. fmt.Println("s_params value", s_params, value)
  136. if strings.ToLower(strings.Trim(value, " ")) == "username" { //picpath, picname, username, newpicname
  137. tempv := params.Get("jwt_username")
  138. s_params = append(s_params, tempv)
  139. } else if strings.ToLower(strings.Trim(value, " ")) == "docname" {
  140. s_params = append(s_params, file.Filename)
  141. } else if strings.ToLower(strings.Trim(value, " ")) == "newdocname" {
  142. s_params = append(s_params, fileName)
  143. } else if strings.ToLower(strings.Trim(value, " ")) == "docpath" {
  144. s_params = append(s_params, dictName) //
  145. } else if strings.ToLower(strings.Trim(value, " ")) == "dictid" {
  146. s_params = append(s_params, dictId) //
  147. fmt.Println("s_params", s_params, dictId)
  148. } else if strings.ToLower(strings.Trim(value, " ")) == "filesize" {
  149. s_params = append(s_params, file.Size) //
  150. } else {
  151. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  152. }
  153. }
  154. }
  155. execresult, err = execDataBySql(sql, s_params)
  156. if err != nil {
  157. fmt.Println("execDataBySql err", err)
  158. appG.Response(http.StatusOK, e.ERROR, err.Error())
  159. }
  160. }
  161. }
  162. context.JSON(http.StatusOK, gin.H{
  163. "hash": md5string,
  164. "filename": fileName,
  165. "origin": file.Filename,
  166. "size": file.Size,
  167. "execresult": execresult,
  168. })
  169. }
  170. /*
  171. *
  172. Upload image handler
  173. */
  174. func UploaderImage(context *gin.Context) {
  175. logging.Info("UploaderImage ", context.Keys)
  176. var (
  177. maxUploadSize = setting.AppSetting.ImageMaxSize // 最大上传大小
  178. distPath string // 最终的输出目录
  179. err error
  180. file *multipart.FileHeader
  181. src multipart.File
  182. dist *os.File
  183. )
  184. // Source
  185. if file, err = context.FormFile("file"); err != nil {
  186. parseFormFail(context)
  187. return
  188. }
  189. sqlname := context.GetHeader("optname")
  190. extname := strings.ToLower(path.Ext(file.Filename))
  191. if isImage(extname) == false {
  192. context.JSON(http.StatusBadRequest, gin.H{
  193. "message": "不支持的上传文件类型: " + extname,
  194. })
  195. return
  196. }
  197. if file.Size > int64(maxUploadSize) {
  198. context.JSON(http.StatusBadRequest, gin.H{
  199. "message": "上传文件太大, 最大文件大小限制为(byte): " + strconv.Itoa(int(maxUploadSize)),
  200. })
  201. return
  202. }
  203. if src, err = file.Open(); err != nil {
  204. }
  205. defer src.Close()
  206. hash := md5.New()
  207. io.Copy(hash, src)
  208. md5string := hex.EncodeToString(hash.Sum([]byte("")))
  209. fileName := md5string + extname
  210. //savePathDir := setting.CurrentPath+setting.AppSetting.ImageSavePath+fileName+"/"
  211. //_,err = os.Stat(savePathDir)
  212. //if err == nil {
  213. // fmt.Println(err)//todo 错误处理
  214. //}
  215. //if os.IsNotExist(err) {
  216. // err:=os.Mkdir(setting.CurrentPath+setting.AppSetting.ImageSavePath+fileName,os.ModePerm)
  217. // if err!=nil{
  218. // fmt.Println(err)
  219. // }//目录不存在则创建
  220. //}
  221. // Destination
  222. picPath := setting.CurrentPath + setting.AppSetting.ImageSavePath + sqlname + "/"
  223. err = PathCheck(picPath) //检查路径并创建
  224. if err != nil {
  225. fmt.Println("PathCheck err", err)
  226. }
  227. distPath = path.Join(picPath, fileName)
  228. if dist, err = os.Create(distPath); err != nil {
  229. fmt.Println("Create err", err)
  230. }
  231. defer dist.Close()
  232. // FIXME: open 2 times
  233. if src, err = file.Open(); err != nil {
  234. //
  235. }
  236. // Copy
  237. io.Copy(dist, src)
  238. // 压缩缩略图
  239. // 不管成功与否,都会进行下一步的返回
  240. if _, err := thumbnailify(distPath, sqlname); err != nil {
  241. }
  242. var execresult interface{}
  243. params := context.Request.Form
  244. if sqlname != "" {
  245. sql, p := restful.GetSqlByNameDB(sqlname)
  246. if sql != "" {
  247. s_params := make([]interface{}, 0)
  248. paramslist := strings.Split(p, ",")
  249. if len(paramslist) > 0 && p != "" {
  250. for _, value := range paramslist {
  251. if strings.ToLower(strings.Trim(value, " ")) == "username" { //picpath, picname, username, newpicname
  252. tempv := params.Get("jwt_username")
  253. s_params = append(s_params, tempv)
  254. } else if strings.ToLower(strings.Trim(value, " ")) == "picname" {
  255. s_params = append(s_params, file.Filename)
  256. } else if strings.ToLower(strings.Trim(value, " ")) == "newpicname" {
  257. s_params = append(s_params, fileName)
  258. } else if strings.ToLower(strings.Trim(value, " ")) == "picpath" {
  259. s_params = append(s_params, sqlname) //全路径加文件名
  260. } else {
  261. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  262. }
  263. }
  264. }
  265. execresult, err = execDataBySql(sql, s_params)
  266. if err != nil {
  267. fmt.Println("execDataBySql err", err)
  268. }
  269. }
  270. }
  271. context.JSON(http.StatusOK, gin.H{
  272. "hash": md5string,
  273. "filename": fileName,
  274. "origin": file.Filename,
  275. "size": file.Size,
  276. "execresult": execresult,
  277. })
  278. }
  279. /*
  280. *
  281. Get file raw
  282. */
  283. func GetFileRaw(context *gin.Context) {
  284. filename := context.Param("filename")
  285. logging.Info("GetFileRaw ", context.Keys, filename)
  286. filePath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  287. if isExistFile := fs.PathExists(filePath); isExistFile == false {
  288. // if the path not found
  289. http.NotFound(context.Writer, context.Request)
  290. return
  291. }
  292. http.ServeFile(context.Writer, context.Request, filePath)
  293. }
  294. //
  295. ///**
  296. //Download a file
  297. //*/
  298. //func DownloadFile(context *gin.Context) {
  299. // filename := context.Param("filename")
  300. // logging.Info("DownloadFile ", context.Keys, filename)
  301. // eqpic, _ := restful.Engine.SQL("SELECT * FROM eq_doc where id = ? ", filename).QueryString()
  302. // if eqpic == nil {
  303. // http.NotFound(context.Writer, context.Request)
  304. // return
  305. // }
  306. // originFilePath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqpic[0]["docpath"], eqpic[0]["newdocname"])
  307. // if fs.PathExists(originFilePath) == false {
  308. // // if the path not found
  309. // http.NotFound(context.Writer, context.Request)
  310. // return
  311. // }
  312. // http.ServeFile(context.Writer, context.Request, originFilePath)
  313. //
  314. //}
  315. //
  316. ///**
  317. //Get Origin image
  318. //*/
  319. //func GetOriginImage(context *gin.Context) {
  320. // //appG := app.Gin{C: context}
  321. // filename := context.Param("filename")
  322. // logging.Info("GetOriginImage ", context.Keys, filename)
  323. // eqpic, _ := restful.Engine.SQL("SELECT * FROM eq_pic where id = ? ", filename).QueryString()
  324. // if eqpic == nil {
  325. // http.NotFound(context.Writer, context.Request)
  326. // return
  327. // }
  328. // originImagePath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, eqpic[0]["picpath"], eqpic[0]["newpicname"])
  329. // if fs.PathExists(originImagePath) == false {
  330. // // if the path not found
  331. // http.NotFound(context.Writer, context.Request)
  332. // return
  333. // }
  334. // http.ServeFile(context.Writer, context.Request, originImagePath)
  335. // //appG.Response(http.StatusOK, e.SUCCESS, eqpic[0]["picname"])
  336. //}
  337. //
  338. ///**
  339. //Get thumbnail image
  340. //*/
  341. //func GetThumbnailImage(context *gin.Context) {
  342. // filename := context.Param("filename")
  343. // logging.Info("GetThumbnailImage ", context.Keys, filename)
  344. // eqpic, _ := restful.Engine.SQL("SELECT * FROM eq_pic where id = ? ", filename).QueryString()
  345. // if eqpic == nil {
  346. // http.NotFound(context.Writer, context.Request)
  347. // return
  348. // }
  349. // thumbnailImagePath := path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, eqpic[0]["picpath"], eqpic[0]["newpicname"])
  350. // originImagePath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, filename)
  351. //
  352. // if fs.PathExists(thumbnailImagePath) == false {
  353. // // if thumbnail image not exist, try to get origin image
  354. // if fs.PathExists(originImagePath) == true {
  355. // http.ServeFile(context.Writer, context.Request, originImagePath)
  356. // return
  357. // }
  358. // // if the path not found
  359. // http.NotFound(context.Writer, context.Request)
  360. // return
  361. // }
  362. // http.ServeFile(context.Writer, context.Request, thumbnailImagePath)
  363. //}
  364. /*
  365. *
  366. Generate thumbnail
  367. */
  368. func thumbnailify(imagePath, subdirectory string) (outputPath string, err error) {
  369. var (
  370. file *os.File
  371. img image.Image
  372. )
  373. Filename := strings.ToLower(path.Base(imagePath))
  374. extname := strings.ToLower(path.Ext(imagePath))
  375. outputPath = setting.CurrentPath + setting.AppSetting.ThumbnailSavePath + subdirectory + "/" + Filename
  376. err = PathCheck(setting.CurrentPath + setting.AppSetting.ThumbnailSavePath + subdirectory)
  377. if err != nil {
  378. fmt.Println("thumbnailify PathCheck err", err)
  379. }
  380. // 读取文件
  381. if file, err = os.Open(imagePath); err != nil {
  382. return
  383. }
  384. defer file.Close()
  385. // decode jpeg into image.Image
  386. switch extname {
  387. case ".jpg", ".jpeg":
  388. img, err = jpeg.Decode(file)
  389. break
  390. case ".png":
  391. img, err = png.Decode(file)
  392. break
  393. case ".gif":
  394. img, err = gif.Decode(file)
  395. break
  396. default:
  397. err = errors.New("Unsupport file type" + extname)
  398. return
  399. }
  400. if img == nil {
  401. err = errors.New("Generate thumbnail fail...")
  402. return
  403. }
  404. m := resize.Thumbnail(uint(setting.AppSetting.ThumbnailMaxWidth), uint(setting.AppSetting.ThumbnailMaxHeight), img, resize.Lanczos3)
  405. out, err := os.Create(outputPath)
  406. if err != nil {
  407. return
  408. }
  409. defer out.Close()
  410. // write new image to file
  411. //decode jpeg/png/gif into image.Image
  412. switch extname {
  413. case ".jpg", ".jpeg":
  414. jpeg.Encode(out, m, nil)
  415. break
  416. case ".png":
  417. png.Encode(out, m)
  418. break
  419. case ".gif":
  420. gif.Encode(out, m, nil)
  421. break
  422. default:
  423. err = errors.New("Unsupport file type" + extname)
  424. return
  425. }
  426. return
  427. }
  428. func UploadFiles(c *gin.Context) {
  429. logging.Info("UploadFiles ", c.Keys)
  430. appG := app.Gin{C: c}
  431. err := c.Request.ParseMultipartForm(200000)
  432. if err != nil {
  433. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  434. return
  435. }
  436. // 获取表单
  437. form := c.Request.MultipartForm
  438. // 获取参数upload后面的多个文件名,存放到数组files里面,
  439. files := form.File["upload"]
  440. // 遍历数组,每取出一个file就拷贝一次
  441. for i, _ := range files {
  442. file, err := files[i].Open()
  443. defer file.Close()
  444. if err != nil {
  445. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  446. return
  447. }
  448. fileName := files[i].Filename
  449. fmt.Println(fileName)
  450. out, err := os.Create(fileName)
  451. defer out.Close()
  452. if err != nil {
  453. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  454. return
  455. }
  456. _, err = io.Copy(out, file)
  457. if err != nil {
  458. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  459. return
  460. }
  461. appG.Response(http.StatusOK, e.SUCCESS, "upload successful \n")
  462. }
  463. }
  464. func PathCheck(path string) (err error) {
  465. b, err := PathExists(path)
  466. if err != nil {
  467. fmt.Println("exist err", err)
  468. }
  469. if !b {
  470. fmt.Println(path, " 目录不存在,重新创建")
  471. err = os.Mkdir(path, 0777)
  472. if err != nil {
  473. fmt.Println("Mkdir err", err)
  474. }
  475. }
  476. return
  477. }
  478. func PathExists(path string) (bool, error) {
  479. _, err := os.Stat(path)
  480. if err == nil {
  481. return true, nil
  482. }
  483. if os.IsNotExist(err) {
  484. return false, nil
  485. }
  486. return false, err
  487. }