upload.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. "../../pkg/app"
  10. "../../pkg/e"
  11. "../../pkg/logging"
  12. "../../pkg/setting"
  13. "../../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. check a file is a image or not
  31. */
  32. func isImage(extName string) bool {
  33. for i := 0; i < len(supportImageExtNames); i++ {
  34. if supportImageExtNames[i] == extName {
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. /**
  41. Handler the parse error
  42. */
  43. func parseFormFail(context *gin.Context) {
  44. context.JSON(http.StatusBadRequest, gin.H{
  45. "msg" : "fail",
  46. "message": "Can not parse form",
  47. })
  48. }
  49. /**
  50. Upload file handler
  51. */
  52. func UploadFile(context *gin.Context) {
  53. appG := app.Gin{C: context}
  54. dictId := context.Param("id")
  55. dictName := context.Param("name")
  56. logging.Info("UploadFile ",context.Keys,dictId,dictName)
  57. var (
  58. isSupportFile bool
  59. maxUploadSize = setting.AppSetting.FileMaxSize // 最大上传大小
  60. allowTypes = setting.AppSetting.FileAllowType // 可上传的文件类型
  61. distPath string // 最终的输出目录
  62. err error
  63. file *multipart.FileHeader
  64. src multipart.File
  65. dist *os.File
  66. )
  67. // Source
  68. if file, err = context.FormFile("file"); err != nil {
  69. parseFormFail(context)
  70. return
  71. }
  72. sqlname := context.GetHeader("optname")
  73. extname := path.Ext(file.Filename)
  74. if len(allowTypes) != 0 {
  75. for i := 0; i < len(allowTypes); i++ {
  76. if allowTypes[i] == extname {
  77. isSupportFile = true
  78. break
  79. }
  80. }
  81. if isSupportFile == false {
  82. context.JSON(http.StatusBadRequest, gin.H{
  83. "message": "不支持的文件类型: " + extname,
  84. })
  85. return
  86. }
  87. }
  88. if file.Size > int64(maxUploadSize) {
  89. context.JSON(http.StatusBadRequest, gin.H{
  90. "message": "上传文件太大, 最大限制为(byte): " + strconv.Itoa(int(maxUploadSize)),
  91. })
  92. return
  93. }
  94. if src, err = file.Open(); err != nil {
  95. // open the file fail...
  96. }
  97. defer src.Close()
  98. hash := md5.New()
  99. io.Copy(hash, src)
  100. md5string := hex.EncodeToString(hash.Sum([]byte("")))
  101. fileName := md5string + extname
  102. // Destination
  103. filePath := setting.CurrentPath+setting.AppSetting.FileSavePath+dictName+"/"
  104. err = PathCheck(filePath) //检查路径并创建
  105. if err != nil {
  106. fmt.Println("PathCheck err",err)
  107. }
  108. distPath = path.Join(filePath, fileName)
  109. if dist, err = os.Create(distPath); err != nil {
  110. fmt.Println("Create err",err)
  111. }
  112. defer dist.Close()
  113. //distPath = setting.CurrentPath + setting.AppSetting.FileSavePath +"/"+ fileName
  114. if dist, err = os.Create(distPath); err != nil {
  115. // create dist file fail...
  116. }
  117. defer dist.Close()
  118. // FIXME: open 2 times
  119. if src, err = file.Open(); err != nil {
  120. //
  121. }
  122. // Copy
  123. io.Copy(dist, src)
  124. var execresult interface{}
  125. params := context.Request.Form
  126. if sqlname != "" {
  127. sql, p := restful.GetSqlByNameDB(sqlname)//todo insertcustomdoc
  128. if sql != ""{
  129. s_params := make([]interface{}, 0)
  130. paramslist := strings.Split(p,",")
  131. if len(paramslist)>0 && p!="" {
  132. for _, value := range paramslist {
  133. fmt.Println("s_params value",s_params,value)
  134. if (strings.ToLower(strings.Trim(value," "))=="username"){//picpath, picname, username, newpicname
  135. tempv := params.Get("jwt_username")
  136. s_params = append(s_params, tempv)
  137. }else if (strings.ToLower(strings.Trim(value," "))=="docname"){
  138. s_params = append(s_params, file.Filename)
  139. }else if (strings.ToLower(strings.Trim(value," "))=="newdocname"){
  140. s_params = append(s_params, fileName)
  141. }else if (strings.ToLower(strings.Trim(value," "))=="docpath"){
  142. s_params = append(s_params, dictName) //
  143. }else if (strings.ToLower(strings.Trim(value," "))=="dictid"){
  144. s_params = append(s_params,dictId) //
  145. fmt.Println("s_params",s_params,dictId)
  146. }else if (strings.ToLower(strings.Trim(value," "))=="filesize"){
  147. s_params = append(s_params, file.Size) //
  148. }else{
  149. s_params = append(s_params, params.Get(strings.Trim(value," ")))
  150. }
  151. }
  152. }
  153. execresult, err = execDataBySql(sql, s_params)
  154. if err != nil{
  155. fmt.Println("execDataBySql err",err)
  156. appG.Response(http.StatusOK, e.ERROR, err.Error())
  157. }
  158. }
  159. }
  160. context.JSON(http.StatusOK, gin.H{
  161. "hash": md5string,
  162. "filename": fileName,
  163. "origin": file.Filename,
  164. "size": file.Size,
  165. "execresult": execresult,
  166. })
  167. }
  168. /**
  169. Upload image handler
  170. */
  171. func UploaderImage(context *gin.Context) {
  172. logging.Info("UploaderImage ",context.Keys)
  173. var (
  174. maxUploadSize = setting.AppSetting.ImageMaxSize // 最大上传大小
  175. distPath string // 最终的输出目录
  176. err error
  177. file *multipart.FileHeader
  178. src multipart.File
  179. dist *os.File
  180. )
  181. // Source
  182. if file, err = context.FormFile("file"); err != nil {
  183. parseFormFail(context)
  184. return
  185. }
  186. sqlname := context.GetHeader("optname")
  187. extname := strings.ToLower(path.Ext(file.Filename))
  188. if isImage(extname) == false {
  189. context.JSON(http.StatusBadRequest, gin.H{
  190. "message": "不支持的上传文件类型: " + extname,
  191. })
  192. return
  193. }
  194. if file.Size > int64(maxUploadSize) {
  195. context.JSON(http.StatusBadRequest, gin.H{
  196. "message": "上传文件太大, 最大文件大小限制为(byte): " + strconv.Itoa(int(maxUploadSize)),
  197. })
  198. return
  199. }
  200. if src, err = file.Open(); err != nil {
  201. }
  202. defer src.Close()
  203. hash := md5.New()
  204. io.Copy(hash, src)
  205. md5string := hex.EncodeToString(hash.Sum([]byte("")))
  206. fileName := md5string + extname
  207. //savePathDir := setting.CurrentPath+setting.AppSetting.ImageSavePath+fileName+"/"
  208. //_,err = os.Stat(savePathDir)
  209. //if err == nil {
  210. // fmt.Println(err)//todo 错误处理
  211. //}
  212. //if os.IsNotExist(err) {
  213. // err:=os.Mkdir(setting.CurrentPath+setting.AppSetting.ImageSavePath+fileName,os.ModePerm)
  214. // if err!=nil{
  215. // fmt.Println(err)
  216. // }//目录不存在则创建
  217. //}
  218. // Destination
  219. picPath := setting.CurrentPath+setting.AppSetting.ImageSavePath+sqlname+"/"
  220. err = PathCheck(picPath) //检查路径并创建
  221. if err != nil {
  222. fmt.Println("PathCheck err",err)
  223. }
  224. distPath = path.Join(picPath, fileName)
  225. if dist, err = os.Create(distPath); err != nil {
  226. fmt.Println("Create err",err)
  227. }
  228. defer dist.Close()
  229. // FIXME: open 2 times
  230. if src, err = file.Open(); err != nil {
  231. //
  232. }
  233. defer src.Close()
  234. // Copy
  235. io.Copy(dist, src)
  236. // 压缩缩略图
  237. // 不管成功与否,都会进行下一步的返回
  238. if _, err := thumbnailify(distPath,sqlname); err != nil {
  239. logging.Error("thumbnailify_err",err)
  240. picThumbnailPath := setting.CurrentPath+setting.AppSetting.ThumbnailSavePath+sqlname+"/"
  241. println(picThumbnailPath)
  242. err = PathCheck(picThumbnailPath) //检查路径并创建
  243. distThumbnailPath := path.Join(picThumbnailPath, fileName)
  244. println(distThumbnailPath)
  245. distThumbnail, err := os.Create(distThumbnailPath)
  246. if err != nil {
  247. fmt.Println("CreateThumbnail err",err)
  248. }
  249. srcThumbnail, err := file.Open()
  250. if err != nil {
  251. //
  252. }
  253. io.Copy(distThumbnail, srcThumbnail)
  254. distThumbnail.Close()
  255. src.Close()
  256. }
  257. var execresult interface{}
  258. params := context.Request.Form
  259. if sqlname != "" {
  260. sql, p := restful.GetSqlByNameDB(sqlname)
  261. if sql != ""{
  262. s_params := make([]interface{}, 0)
  263. paramslist := strings.Split(p,",")
  264. if len(paramslist)>0 && p!="" {
  265. for _, value := range paramslist {
  266. if (strings.ToLower(strings.Trim(value," "))=="username"){//picpath, picname, username, newpicname
  267. tempv := params.Get("jwt_username")
  268. s_params = append(s_params, tempv)
  269. }else if (strings.ToLower(strings.Trim(value," "))=="picname"){
  270. s_params = append(s_params, file.Filename)
  271. }else if (strings.ToLower(strings.Trim(value," "))=="newpicname"){
  272. s_params = append(s_params, fileName)
  273. }else if (strings.ToLower(strings.Trim(value," "))=="picpath"){
  274. s_params = append(s_params, sqlname) //全路径加文件名
  275. } else{
  276. s_params = append(s_params, params.Get(strings.Trim(value," ")))
  277. }
  278. }
  279. }
  280. execresult, err = execDataBySql(sql, s_params)
  281. if err != nil{
  282. fmt.Println("execDataBySql err",err)
  283. }
  284. }
  285. }
  286. context.JSON(http.StatusOK, gin.H{
  287. "hash": md5string,
  288. "filename": fileName,
  289. "origin": file.Filename,
  290. "size": file.Size,
  291. "execresult": execresult,
  292. })
  293. }
  294. /**
  295. Upload image handler
  296. */
  297. func UploaderTmrImage(context *gin.Context) {
  298. logging.Info("UploaderTmrImage ",context.Keys)
  299. var (
  300. maxUploadSize = setting.AppSetting.ImageMaxSize // 最大上传大小
  301. distPath string // 最终的输出目录
  302. err error
  303. file *multipart.FileHeader
  304. src multipart.File
  305. dist *os.File
  306. )
  307. // Source
  308. if file, err = context.FormFile("file"); err != nil {
  309. parseFormFail(context)
  310. return
  311. }
  312. pastureid := context.GetHeader("pastureid") //牧场id
  313. projuctid := context.GetHeader("projuctid") // 主计划id
  314. pfid := context.GetHeader("pfid") // 配方id
  315. optid := context.GetHeader("optid") //子计划id
  316. catchtime := context.GetHeader("catchtime") // 捕获时间
  317. picclass := context.GetHeader("picclass") // 图片分类
  318. extname := strings.ToLower(path.Ext(file.Filename))
  319. if isImage(extname) == false {
  320. context.JSON(http.StatusBadRequest, gin.H{
  321. "msg" : "fail",
  322. "message": "不支持的上传文件类型: " + extname,
  323. })
  324. return
  325. }
  326. if file.Size > int64(maxUploadSize) {
  327. context.JSON(http.StatusBadRequest, gin.H{
  328. "msg" : "fail",
  329. "message": "上传文件太大, 最大文件大小限制为(byte): " + strconv.Itoa(int(maxUploadSize)),
  330. })
  331. return
  332. }
  333. if src, err = file.Open(); err != nil {
  334. }
  335. defer src.Close()
  336. hash := md5.New()
  337. io.Copy(hash, src)
  338. md5string := hex.EncodeToString(hash.Sum([]byte("")))
  339. fileName := md5string + extname
  340. //savePathDir := setting.CurrentPath+setting.AppSetting.ImageSavePath+fileName+"/"
  341. //_,err = os.Stat(savePathDir)
  342. //if err == nil {
  343. // fmt.Println(err)//todo 错误处理
  344. //}
  345. //if os.IsNotExist(err) {
  346. // err:=os.Mkdir(setting.CurrentPath+setting.AppSetting.ImageSavePath+fileName,os.ModePerm)
  347. // if err!=nil{
  348. // fmt.Println(err)
  349. // }//目录不存在则创建
  350. //}
  351. // Destination
  352. picPath := setting.CurrentPath+setting.AppSetting.ImageSavePath+projuctid+"/"
  353. err = PathCheck(picPath) //检查路径并创建
  354. if err != nil {
  355. fmt.Println("PathCheck err",err)
  356. }
  357. distPath = path.Join(picPath, fileName)
  358. if dist, err = os.Create(distPath); err != nil {
  359. fmt.Println("Create err",err)
  360. }
  361. defer dist.Close()
  362. // FIXME: open 2 times
  363. if src, err = file.Open(); err != nil {
  364. //
  365. }
  366. defer src.Close()
  367. // Copy
  368. io.Copy(dist, src)
  369. // 压缩缩略图
  370. // 不管成功与否,都会进行下一步的返回
  371. if _, err := thumbnailify(distPath,projuctid); err != nil {
  372. logging.Error("thumbnailify_err",err)
  373. picThumbnailPath := setting.CurrentPath+setting.AppSetting.ThumbnailSavePath+projuctid+"/"
  374. println(picThumbnailPath)
  375. err = PathCheck(picThumbnailPath) //检查路径并创建
  376. distThumbnailPath := path.Join(picThumbnailPath, fileName)
  377. println(distThumbnailPath)
  378. distThumbnail, err := os.Create(distThumbnailPath)
  379. if err != nil {
  380. fmt.Println("CreateThumbnail err",err)
  381. }
  382. srcThumbnail, err := file.Open()
  383. if err != nil {
  384. //
  385. }
  386. io.Copy(distThumbnail, srcThumbnail)
  387. distThumbnail.Close()
  388. src.Close()
  389. }
  390. var execresult interface{}
  391. params := context.Request.Form
  392. sqlname := "insertcustompic"
  393. if sqlname != "" {
  394. sql, p := restful.GetSqlByNameDB(sqlname)
  395. if sql != ""{
  396. s_params := make([]interface{}, 0)
  397. paramslist := strings.Split(p,",")
  398. if len(paramslist)>0 && p!="" {
  399. for _, value := range paramslist {
  400. switch strings.ToLower(strings.Trim(value," ")) {
  401. case "username":
  402. tempv := params.Get("jwt_username")
  403. s_params = append(s_params, tempv)
  404. case "picname":
  405. s_params = append(s_params, file.Filename)
  406. case "newpicname":
  407. s_params = append(s_params, fileName)
  408. case "picpath":
  409. s_params = append(s_params, projuctid)
  410. case "pastureid":
  411. s_params = append(s_params, pastureid)
  412. case "projuctid":
  413. s_params = append(s_params, projuctid)
  414. case "pfid":
  415. s_params = append(s_params, pfid)
  416. case "optid":
  417. s_params = append(s_params, optid)
  418. case "catchtime":
  419. s_params = append(s_params, catchtime)
  420. case "picclass":
  421. s_params = append(s_params, picclass)
  422. default:
  423. s_params = append(s_params, params.Get(strings.Trim(value," ")))
  424. }
  425. }
  426. }
  427. execresult, err = execDataBySql(sql, s_params)
  428. if err != nil{
  429. fmt.Println("execDataBySql err",err)
  430. }
  431. }
  432. }
  433. context.JSON(http.StatusOK, gin.H{
  434. "msg" : "ok",
  435. "hash": md5string,
  436. "filename": fileName,
  437. "origin": file.Filename,
  438. "size": file.Size,
  439. "execresult": execresult,
  440. })
  441. }
  442. /**
  443. Get file raw
  444. */
  445. func GetFileRaw(context *gin.Context) {
  446. filename := context.Param("filename")
  447. logging.Info("GetFileRaw ",context.Keys,filename)
  448. filePath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  449. if isExistFile := fs.PathExists(filePath); isExistFile == false {
  450. // if the path not found
  451. http.NotFound(context.Writer, context.Request)
  452. return
  453. }
  454. http.ServeFile(context.Writer, context.Request, filePath)
  455. }
  456. /**
  457. Download a file
  458. */
  459. func DownloadFile(context *gin.Context) {
  460. filename := context.Param("filename")
  461. logging.Info("DownloadFile ",context.Keys,filename)
  462. eqpic, _ := restful.Engine.SQL("SELECT * FROM eq_doc where id = ? ", filename).QueryString()
  463. if eqpic == nil {
  464. http.NotFound(context.Writer, context.Request)
  465. return
  466. }
  467. originFilePath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath,eqpic[0]["docpath"],eqpic[0]["newdocname"])
  468. if fs.PathExists(originFilePath) == false {
  469. // if the path not found
  470. http.NotFound(context.Writer, context.Request)
  471. return
  472. }
  473. http.ServeFile(context.Writer, context.Request, originFilePath)
  474. }
  475. /**
  476. Get Origin image
  477. */
  478. func GetOriginImage(context *gin.Context) {
  479. //appG := app.Gin{C: context}
  480. filename := context.Param("filename")
  481. logging.Info("GetOriginImage ",context.Keys,filename)
  482. eqpic, _ := restful.Engine.SQL("SELECT * FROM eq_pic where id = ? ", filename).QueryString()
  483. if eqpic == nil {
  484. http.NotFound(context.Writer, context.Request)
  485. return
  486. }
  487. originImagePath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath,eqpic[0]["picpath"],eqpic[0]["newpicname"])
  488. if fs.PathExists(originImagePath) == false {
  489. // if the path not found
  490. http.NotFound(context.Writer, context.Request)
  491. return
  492. }
  493. http.ServeFile(context.Writer, context.Request, originImagePath)
  494. //appG.Response(http.StatusOK, e.SUCCESS, eqpic[0]["picname"])
  495. }
  496. /**
  497. Get thumbnail image
  498. */
  499. func GetThumbnailImage(context *gin.Context) {
  500. filename := context.Param("filename")
  501. logging.Info("GetThumbnailImage ",context.Keys,filename)
  502. eqpic, _ := restful.Engine.SQL("SELECT * FROM eq_pic where id = ? ", filename).QueryString()
  503. if eqpic == nil {
  504. http.NotFound(context.Writer, context.Request)
  505. return
  506. }
  507. thumbnailImagePath := path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath,eqpic[0]["picpath"],eqpic[0]["newpicname"])
  508. originImagePath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, filename)
  509. if fs.PathExists(thumbnailImagePath) == false {
  510. // if thumbnail image not exist, try to get origin image
  511. if fs.PathExists(originImagePath) == true {
  512. http.ServeFile(context.Writer, context.Request, originImagePath)
  513. return
  514. }
  515. // if the path not found
  516. http.NotFound(context.Writer, context.Request)
  517. return
  518. }
  519. http.ServeFile(context.Writer, context.Request, thumbnailImagePath)
  520. }
  521. /**
  522. Generate thumbnail
  523. */
  524. func thumbnailify(imagePath,subdirectory string) (outputPath string, err error) {
  525. var (
  526. file *os.File
  527. img image.Image
  528. )
  529. Filename := strings.ToLower(path.Base(imagePath))
  530. extname := strings.ToLower(path.Ext(imagePath))
  531. outputPath = setting.CurrentPath + setting.AppSetting.ThumbnailSavePath +subdirectory+ "/" + Filename
  532. err = PathCheck(setting.CurrentPath + setting.AppSetting.ThumbnailSavePath +subdirectory)
  533. if err !=nil{
  534. fmt.Println("thumbnailify PathCheck err",err)
  535. }
  536. // 读取文件
  537. if file, err = os.Open(imagePath); err != nil {
  538. return
  539. }
  540. defer file.Close()
  541. // decode jpeg into image.Image
  542. switch extname {
  543. case ".jpg", ".jpeg":
  544. img, err = jpeg.Decode(file)
  545. break
  546. case ".png":
  547. img, err = png.Decode(file)
  548. break
  549. case ".gif":
  550. img, err = gif.Decode(file)
  551. break
  552. default:
  553. err = errors.New("Unsupport file type" + extname)
  554. return
  555. }
  556. if img == nil {
  557. err = errors.New("Generate thumbnail fail...")
  558. return
  559. }
  560. m := resize.Thumbnail(uint( setting.AppSetting.ThumbnailMaxWidth), uint(setting.AppSetting.ThumbnailMaxHeight), img, resize.Lanczos3)
  561. out, err := os.Create(outputPath)
  562. if err != nil {
  563. return
  564. }
  565. defer out.Close()
  566. // write new image to file
  567. //decode jpeg/png/gif into image.Image
  568. switch extname {
  569. case ".jpg", ".jpeg":
  570. jpeg.Encode(out, m, nil)
  571. break
  572. case ".png":
  573. png.Encode(out, m)
  574. break
  575. case ".gif":
  576. gif.Encode(out, m, nil)
  577. break
  578. default:
  579. err = errors.New("Unsupport file type" + extname)
  580. return
  581. }
  582. return
  583. }
  584. func UploadFiles(c *gin.Context) {
  585. logging.Info("UploadFiles ",c.Keys)
  586. appG := app.Gin{C: c}
  587. err := c.Request.ParseMultipartForm(200000)
  588. if err != nil {
  589. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  590. return
  591. }
  592. // 获取表单
  593. form := c.Request.MultipartForm
  594. // 获取参数upload后面的多个文件名,存放到数组files里面,
  595. files := form.File["upload"]
  596. // 遍历数组,每取出一个file就拷贝一次
  597. for i, _ := range files {
  598. file, err := files[i].Open()
  599. defer file.Close()
  600. if err != nil {
  601. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  602. return
  603. }
  604. fileName := files[i].Filename
  605. fmt.Println(fileName)
  606. out, err := os.Create(fileName)
  607. defer out.Close()
  608. if err != nil {
  609. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  610. return
  611. }
  612. _, err = io.Copy(out, file)
  613. if err != nil {
  614. appG.Response(http.StatusOK, e.ERROR_IMPORT_FAIL, err)
  615. return
  616. }
  617. appG.Response(http.StatusOK, e.SUCCESS, "upload successful \n")
  618. }
  619. }
  620. func PathCheck(path string)(err error){
  621. b,err :=PathExists(path)
  622. if err != nil {
  623. fmt.Println("exist err",err)
  624. }
  625. if !b{
  626. fmt.Println(path," 目录不存在,重新创建")
  627. err = os.Mkdir(path, 0777)
  628. if err != nil {
  629. fmt.Println("Mkdir err",err)
  630. }
  631. }
  632. return
  633. }
  634. func PathExists(path string) (bool, error) {
  635. _, err := os.Stat(path)
  636. if err == nil {
  637. return true, nil
  638. }
  639. if os.IsNotExist(err) {
  640. return false, nil
  641. }
  642. return false, err
  643. }