Guest User

Untitled

a guest
Jul 18th, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.23 KB | None | 0 0
  1. func (a *ItemController) create(c echo.Context) error {
  2.     cc := c.(*middleware.CustomContext)
  3.     db := cc.DB
  4.  
  5.     payload := new(itemPayload)
  6.  
  7.     if validates := util.ValidatePayload(c, payload); !validates {
  8.         return c.NoContent(http.StatusBadRequest)
  9.     }
  10.  
  11.     // Parse the tags manually, binding doesn't work
  12.     rawTags := cc.FormValue("tags")
  13.     stringTags := strings.Split(rawTags, ",")
  14.  
  15.     tagIDs := make([]int, 0, len(stringTags))
  16.  
  17.     for _, t := range stringTags {
  18.         i, err := strconv.ParseInt(t, 10, 64)
  19.         if err != nil {
  20.             continue
  21.         }
  22.         tagIDs = append(tagIDs, int(i))
  23.     }
  24.  
  25.     fileZip, err := cc.FormFile("zip")
  26.  
  27.     if err != nil {
  28.         fmt.Println("error opening form file:", err)
  29.         return c.NoContent(http.StatusInternalServerError)
  30.     }
  31.  
  32.     fileCover, err := cc.FormFile("cover")
  33.  
  34.     if err != nil {
  35.         fmt.Println("error opening form file:", err)
  36.         return c.NoContent(http.StatusInternalServerError)
  37.     }
  38.  
  39.     item, err := models.CreateItem(db, payload, tagIDs)
  40.  
  41.     if err != nil {
  42.         return c.NoContent(http.StatusInternalServerError)
  43.     }
  44.  
  45.     // Create media dir
  46.     err = os.MkdirAll(item.GetMediaDir(), os.ModePerm)
  47.  
  48.     if err != nil {
  49.         fmt.Println("error creating item media dir:", err)
  50.         return c.NoContent(http.StatusInternalServerError)
  51.     }
  52.  
  53.     srcZip, err := fileZip.Open()
  54.     if err != nil {
  55.         return c.NoContent(http.StatusInternalServerError)
  56.     }
  57.     defer srcZip.Close()
  58.  
  59.     // Copy item files
  60.     dstZip, err := os.Create(item.GetMediaDir() + "files.zip")
  61.     if err != nil {
  62.         return c.NoContent(http.StatusInternalServerError)
  63.     }
  64.     defer dstZip.Close()
  65.  
  66.     if _, err = io.Copy(dstZip, srcZip); err != nil {
  67.         fmt.Println("error copying srczip to dstzip:", err)
  68.         return c.NoContent(http.StatusInternalServerError)
  69.     }
  70.  
  71.     // Copy item cover
  72.     srcCover, err := fileCover.Open()
  73.     if err != nil {
  74.         return c.NoContent(http.StatusInternalServerError)
  75.     }
  76.     defer srcCover.Close()
  77.  
  78.     dstCover, err := os.Create(item.GetMediaDir() + "cover")
  79.     if err != nil {
  80.         return c.NoContent(http.StatusInternalServerError)
  81.     }
  82.     defer dstCover.Close()
  83.  
  84.     if _, err = io.Copy(dstCover, srcCover); err != nil {
  85.         fmt.Println("error copying srcCover to dstCover:", err)
  86.         return c.NoContent(http.StatusInternalServerError)
  87.     }
  88.  
  89.     return c.JSON(http.StatusCreated, item)
  90. }
Advertisement
Add Comment
Please, Sign In to add comment