Пример #1
0
func (h *RawFileHandler) Handle(req shared.IRequest) (shared.IResponse, error) {
	w, _ := req.GetRaw()
	file, err := os.Open(h.WebRoot + "/" + h.Filename)
	if err != nil {
		fmt.Fprintln(w, err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		return nil, nil
	}
	defer file.Close()
	io.Copy(w, file)
	return nil, nil
}
Пример #2
0
func (wh *wrappedHandler) Handle(request shared.IRequest) (shared.IResponse, error) {

	requestData := wh.handler.RequestDataPlaceholder()
	_, r := request.GetRaw()

	// Decode request body JSON to object
	dec := json.NewDecoder(r.Body)
	err := dec.Decode(requestData)
	if err != nil {
		return nil, err
	}

	// Do the request
	return wh.handler.Handle(request, requestData)

}
Пример #3
0
func (h *FileHandler) Upload(request shared.IRequest) error {

	var functionName string
	var fileCollection string
	var collectionRef string
	var collectionId uint64

	err := request.URLMatch(&functionName, &fileCollection, &collectionRef, &collectionId)
	if err != nil {
		return err
	}

	_, r := request.GetRaw()
	if r.Method != "POST" && r.Method != "PUT" {
		request.WriteString("Must post a file (1)")
		return nil
	}

	mpr, err := r.MultipartReader()
	if err != nil {
		return err
	}

	var part *multipart.Part
	for {
		thisPart, err := mpr.NextPart()
		if err != nil {
			break
		}
		if thisPart.FormName() == "attachment" {
			part = thisPart
			break
		}
	}
	if part == nil {
		request.WriteString("Must post a file (2)")
		return nil
	}

	origName := part.FileName()

	randBytes := make([]byte, 22, 22)
	_, _ = rand.Reader.Read(randBytes)
	fileName := hex.EncodeToString(randBytes)

	bucket, err := h.getBucket()
	if err != nil {
		return err
	}

	upload, err := ioutil.ReadAll(part)
	if err != nil {
		return err
	}
	err = bucket.Put(h.Path+fileName, upload, "application/octet-stream", s3.Private)
	if err != nil {
		return err
	}
	log.Println("File Written")

	dbEntry := map[string]interface{}{
		collectionRef: collectionId,
		"file":        fileName,
		"filename":    origName,
	}

	err = h.writeDatabaseEntry(request, dbEntry, fileCollection)
	if err != nil {
		return err
	}

	request.WriteString(`
		<script type='text/javascript'>
		window.top.file_done()
		</script>
		Uploaded Successfully.
	`)
	return nil
}