Пример #1
0
func TestUploadHandler(t *testing.T) {
	file := []byte("These are the file contents")
	resp, _ := http.Post(
		"http://localhost:8000/upload/?sessionKey="+client.SessionKey,
		"",
		bytes.NewBuffer(file),
	)
	contents, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != 200 {
		err := fmt.Errorf(string(contents))
		t.Error(err)
	}
	h := sha256.New()
	h.Write(file)
	byteString := h.Sum(nil)
	sha256String := hex.EncodeToString(byteString)

	url, _ := s3.GenerateSignedUrl(sha256String)

	resp, _ = http.Get(url)

	contents, _ = ioutil.ReadAll(resp.Body)

	if string(contents) != string(file) {
		t.Fail()
	}
}
Пример #2
0
func FileDownloadHandler(w http.ResponseWriter, req *http.Request,
	client structs.Client) {
	httpError := httpError{responseWriter: w}
	httpError.code = http.StatusInternalServerError

	var user structs.User
	query := model.DB.Model(&client).Related(&user)
	httpError.err = query.Error
	if httpError.check() {
		return
	}

	fileHash := req.FormValue("fileHash")
	if fileHash == "" {
		w.WriteHeader(http.StatusNotAcceptable)
		return
	}

	var file structs.File
	query = model.DB.Where(
		&structs.File{
			UserId: user.Id,
			Hash:   fileHash,
		},
	).First(&file)
	if query.Error != nil {
		if query.Error == gorm.RecordNotFound {
			w.WriteHeader(http.StatusUnauthorized)
		} else {
			w.WriteHeader(http.StatusInternalServerError)
			w.Write([]byte(query.Error.Error()))
		}
		return
	}

	var exists bool
	exists, httpError.err = s3.TestKeyExistence(fileHash)
	if httpError.check() {
		return
	}

	if exists != true {
		w.WriteHeader(http.StatusNoContent)
		return
	}

	var url string
	url, httpError.err = s3.GenerateSignedUrl(fileHash)
	if httpError.check() {
		return
	}
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(url))
}
Пример #3
0
func DownloadHandler(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	id := vars["id"]
	intId, _ := strconv.Atoi(id)
	int64Id := int64(intId)

	var file structs.File
	query := model.DB.First(&file, int64Id)
	_ = query
	url, err := s3.GenerateSignedUrl(file.Hash)
	resp, err := http.Get(url)
	// keyBytes, err := ioutil.ReadAll(resp.Body)
	w.Header().Add("Content-Type", "application/octet-stream")
	io.Copy(w, resp.Body)
	_ = err
	// w.Write(keyBytes)

	// w.Write()
	// fmt.Println(query.Error)
}