func programUpload(r render.Render, params martini.Params, req *http.Request, w http.ResponseWriter, db *mgo.Database) { dir, err := filepath.Abs(filepath.Dir(os.Args[0])) util.CheckErr(err) err = req.ParseMultipartForm(100000) util.CheckErr(err) //get a ref to the parsed multipart form m := req.MultipartForm //get the *fileheaders // fmt.Println(json.Marshal(m)) files := m.File["file_data"] filenames := make([]string, len(files)) log.Println(len(files)) for i, _ := range files { version := version(db) + 1 //for each fileheader, get a handle to the actual file file, _ := files[i].Open() defer file.Close() //create destination file making sure the path is writeable. ext := filepath.Ext(files[i].Filename) newname := strconv.Itoa(version) newfilename := newname + ext fmt.Println(dir + "/static/program/" + newfilename) dst, _ := os.Create(dir + "/static/program/" + newfilename) defer dst.Close() if _, err := io.Copy(dst, file); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Println("version==", version) program := bson.M{"_id": version, "version": version, "src": "/program/" + newfilename} str, _ := json.Marshal(program) filenames = append(filenames, string(str)) //log.Println(string(str)) //filenames = append(filenames, newfilename) db.C("video_program").Insert(program) db.C("video_program_version").Insert(bson.M{"_id": "version", "version": version}) db.C("video_program_version").Update(bson.M{"_id": "version"}, bson.M{"$set": bson.M{"version": version}}) } log.Printf("%v\r\n", filenames) r.JSON(200, filenames) }
func imgupload(r render.Render, params martini.Params, req *http.Request, w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "POST,OPTIONS") dir, err := filepath.Abs(filepath.Dir(os.Args[0])) util.CheckErr(err) err = req.ParseMultipartForm(100000) util.CheckErr(err) //get a ref to the parsed multipart form m := req.MultipartForm //get the *fileheaders fmt.Println(json.Marshal(m)) files := m.File["file_data"] filenames := []string{} for i, _ := range files { //for each fileheader, get a handle to the actual file file, _ := files[i].Open() defer file.Close() //create destination file making sure the path is writeable. ext := filepath.Ext(files[i].Filename) newfilename := uuid.NewV4().String() + ext if _, err := os.Stat(newfilename); err == nil { newfilename = uuid.NewV4().String() + ext } fmt.Println(dir + "/static/upload/" + newfilename) dst, _ := os.Create(dir + "/static/upload/" + newfilename) filenames = append(filenames, "/upload/"+newfilename) defer dst.Close() if _, err := io.Copy(dst, file); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } ret := map[string]string{"urls": strings.Join(filenames, ",")} r.JSON(200, ret) }
func videoupload(r render.Render, params martini.Params, req *http.Request, w http.ResponseWriter, db *mgo.Database) { dir, err := filepath.Abs(filepath.Dir(os.Args[0])) util.CheckErr(err) err = req.ParseMultipartForm(100000) util.CheckErr(err) //get a ref to the parsed multipart form m := req.MultipartForm //get the *fileheaders // fmt.Println(json.Marshal(m)) files := m.File["file_data"] filenames := []string{} for i, _ := range files { //for each fileheader, get a handle to the actual file file, _ := files[i].Open() defer file.Close() //create destination file making sure the path is writeable. ext := filepath.Ext(files[i].Filename) newname := uuid.NewV4().String() newfilename := newname + ext if _, err := os.Stat(newfilename); err == nil { newfilename = uuid.NewV4().String() + ext } fmt.Println(dir + "/static/uploadvideo/" + newfilename) dst, _ := os.Create(dir + "/static/uploadvideo/" + newfilename) filenames = append(filenames, newfilename) defer dst.Close() if _, err := io.Copy(dst, file); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } db.C("video_list").Insert(bson.M{"_id": newfilename, "name": newfilename, "src": "/uploadvideo/" + newfilename}) } r.JSON(200, filenames) }