示例#1
0
func handleUpload(registry kit.Registry, tmpPath string, r *http.Request) ([]string, apperror.Error) {
	reader, err := r.MultipartReader()
	if err != nil {
		return nil, apperror.Wrap(err, "multipart_error")
	}

	files := make([]string, 0)

	for {
		part, err := reader.NextPart()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return nil, apperror.Wrap(err, "read_error")
			}
		}

		filename := part.FileName()
		if filename == "" {
			// Not a file?
			continue
		}

		id := utils.UUIdv4()
		path := tmpPath + string(os.PathSeparator) + id

		if err := os.MkdirAll(path, 0777); err != nil {
			return nil, apperror.Wrap(err, "create_dir_failed")
		}

		filename = utils.Canonicalize(filename)
		if filename == "" {
			filename = id
		}

		filePath := path + string(os.PathSeparator) + filename

		file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			return nil, apperror.Wrap(err, "file_create_failed")
		}
		defer file.Close()

		_, err = io.Copy(file, part)
		if err != nil {
			return nil, apperror.Wrap(err, "file_copy_failed")
		}

		files = append(files, id+string(os.PathSeparator)+filename)
	}

	return files, nil
}
示例#2
0
文件: fs.go 项目: app-kit/go-appkit
func (fs *Fs) key(rawKey string) string {
	return utils.Canonicalize(rawKey)
}