Example #1
0
func UploadHandler(w http.ResponseWriter, req *http.Request,
	client structs.Client) {
	httpError := httpError{responseWriter: w}

	var contents []byte
	contents, httpError.err = ioutil.ReadAll(req.Body)
	httpError.code = http.StatusUnauthorized
	if httpError.check() {
		return
	}

	h := sha256.New()
	_, httpError.err = h.Write(contents)
	httpError.code = http.StatusInternalServerError
	if httpError.check() {
		return
	}
	byteString := h.Sum(nil)
	sha256String := hex.EncodeToString(byteString)

	// we have the hash, so we might as well check if it
	// exists again before we upload
	var exists bool
	exists, httpError.err = s3.TestKeyExistence(sha256String)
	if httpError.check() {
		return
	}
	if exists == false {
		httpError.err = s3.UploadFile(sha256String, contents)
		if httpError.check() {
			return
		}
	}
	w.WriteHeader(http.StatusOK)
}
Example #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))
}
Example #3
0
func FileActionsHandler(w http.ResponseWriter, req *http.Request,
	client structs.Client) {
	httpError := httpError{responseWriter: w}

	var contents []byte
	contents, httpError.err = ioutil.ReadAll(req.Body)
	httpError.code = http.StatusInternalServerError
	if httpError.check() {
		return
	}

	var fileActions []structs.FileAction
	httpError.err = json.Unmarshal(contents, &fileActions)
	httpError.code = http.StatusNotAcceptable
	if httpError.check() {
		return
	}

	for _, value := range fileActions {
		value.ClientId = client.Id
	}

	fileActions, httpError.err = boxtools.WriteFileActionsToDatabase(fileActions, client)
	httpError.code = http.StatusInternalServerError
	if httpError.check() {
		return
	}

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

	Pusher.Notify(client.SessionKey)

	errs := boxtools.ApplyFileActionsToFileSystemFileTable(fileActions, user)
	if len(errs) != 0 {
		fmt.Println(errs)
	}

	var hashesThatNeedToBeUploaded []string
	hashMap := make(map[string]bool)
	// write to a map to remove any duplicate hashes
	for _, value := range fileActions {
		if value.IsCreate == true {
			hashMap[value.File.Hash] = true
		}
	}
	for key, _ := range hashMap {
		var exists bool
		exists, httpError.err = s3.TestKeyExistence(key)
		httpError.code = http.StatusInternalServerError
		if httpError.check() {
			return
		}
		if exists == false {
			hashesThatNeedToBeUploaded = append(
				hashesThatNeedToBeUploaded,
				key,
			)
		}
	}

	var jsonBytes []byte
	jsonBytes, httpError.err = json.Marshal(hashesThatNeedToBeUploaded)
	httpError.code = http.StatusInternalServerError
	if httpError.check() {
		return
	}
	w.WriteHeader(http.StatusOK)
	w.Write(jsonBytes)
}