Exemplo n.º 1
0
func (h *Handler) checkPart(w http.ResponseWriter, r *http.Request) {
	sid, err := session.Sid(r)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	chunkDirPath := h.rootPath + "storage/.upload/" + sid + "/" + r.FormValue("flowChunkNumber")

	stat, err := os.Stat(chunkDirPath)
	if err != nil {
		h.log.Debug(err)
		http.Error(w, "Internal server error", http.StatusNoContent)
		return
	}

	expectedChunkSize, err := strconv.Atoi(r.URL.Query().Get("flowCurrentChunkSize"))
	if err != nil {
		h.log.Debug(err)
		http.Error(w, "Internal server error", http.StatusBadRequest)
		return
	}
	if int(stat.Size()) != expectedChunkSize {
		h.log.Debug(err)
		http.Error(w, "Chunk size check failed", http.StatusPartialContent)
		return
	}
}
Exemplo n.º 2
0
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	sid, err := session.Sid(r)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	urlParts := strings.Split(r.URL.Path, "/")
	filename := urlParts[len(urlParts)-1]

	requestedPath := h.rootPath + "storage/datastore/" + sid + "/" + filename

	file, err := os.Open(requestedPath)
	if err != nil {
		h.log.Error(err)
		http.Error(w, err.Error(), http.StatusNotFound)
		return
	}

	stat, err := file.Stat()
	if err != nil {
		h.log.Error(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	h.log.Debugf("Download %v/%v", sid, filename)

	w.Header().Set("Content-Disposition", "attachment; filename="+filename)
	w.Header().Set("Content-Type", "application/octet-stream")

	http.ServeContent(w, r, requestedPath, stat.ModTime(), file)
}
Exemplo n.º 3
0
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	sid, err := session.Sid(r)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	dd := &downloadData{}

	var success sql.NullInt64
	var encodingErr sql.NullString

	err = h.db.QueryRow("SELECT success, encode_error FROM file WHERE sid=? ", sid).Scan(&success, &encodingErr)
	switch {
	case err == sql.ErrNoRows:
		dd.Procede = false
		dd.Err = "No encoding job found"
		h.log.Errorf("No encoding job found for sid=%v", sid)
		break

	case err != nil:
		h.log.Error(err)
		dd.Procede = false
		dd.Err = err.Error()
		break

	default:
		if success.Int64 > 0 {
			dd.Procede = true
			dd.Err = encodingErr.String
			dd.First_frame_jpg = "/download/encoded.jpg"
			dd.Mp4_link = "/download/encoded.mp4"
			dd.Ogv_link = "/download/encoded.ogg"
			dd.Webm_link = "/download/encoded.webm"
		} else if len(encodingErr.String) > 0 {
			dd.Procede = false
			dd.Err = encodingErr.String
			janitor.CleanupUser(sid)
		} else {
			dd.Procede = false
		}
	}

	js, err := json.Marshal(dd)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}
Exemplo n.º 4
0
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	sid, err := session.Sid(r)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	err = janitor.CleanupUser(sid)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	h.log.Debugf("User %v cleanup on damand", sid)
}
Exemplo n.º 5
0
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	sid, err := session.Sid(r)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	hasFileInUpload, err := janitor.HasFileInUpload(sid)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	if hasFileInUpload == false {
		h.log.Error("No upload to check for client " + sid)
		http.Error(w, "No file on server", http.StatusNotFound)
		return
	}

	ok, mediaInfo, res, err := janitor.PossibleToEncode(sid)
	errorString := ""
	if err != nil {
		errorString = err.Error()
	}
	returnValue := VideoInfo{
		Procede:          ok,
		Err:              errorString,
		OutputDimensions: res,
		OriginalInfo:     mediaInfo}

	js, err := json.Marshal(returnValue)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}
Exemplo n.º 6
0
func (h *Handler) streamUpload(w http.ResponseWriter, r *http.Request) {
	sid, err := session.Sid(r)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	buf := new(bytes.Buffer)
	reader, err := r.MultipartReader()
	// Part 1: Chunk Number
	// Part 4: Total Size (bytes)
	// Part 6: File Name
	// Part 8: Total Chunks
	// Part 9: Chunk Data
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	part, err := reader.NextPart() // 1
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}
	io.Copy(buf, part)
	chunkNo := buf.String()
	buf.Reset()

	for i := 0; i < 3; i++ { // 2 3 4
		// move through unused parts
		part, err = reader.NextPart()
		if err != nil {
			h.log.Error(err)
			http.Error(w, "Internal server error", http.StatusInternalServerError)
			return
		}
	}

	io.Copy(buf, part)
	flowTotalSize := buf.String()
	buf.Reset()

	for i := 0; i < 2; i++ { // 5 6
		// move through unused parts
		part, err = reader.NextPart()
		if err != nil {
			h.log.Error(err)
			http.Error(w, "Internal server error", http.StatusInternalServerError)
			return
		}
	}

	io.Copy(buf, part)
	fileName := buf.String()
	buf.Reset()

	if chunkNo == "1" {
		err = janitor.RecordFilename(sid, fileName)
		if err != nil {
			h.log.Error(err)
			http.Error(w, "Internal server error", http.StatusInternalServerError)
			return
		}
	}

	for i := 0; i < 3; i++ { // 7 8 9
		// move through unused parts
		part, err = reader.NextPart()
		if err != nil {
			h.log.Error(err)
			http.Error(w, "Internal server error", http.StatusInternalServerError)
			return
		}
	}

	chunkDirPath := h.rootPath + "/storage/.upload/" + sid
	err = os.MkdirAll(chunkDirPath, 02750)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	dst, err := os.Create(chunkDirPath + "/" + chunkNo)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}
	defer dst.Close()
	io.Copy(dst, part)

	fileInfos, err := ioutil.ReadDir(chunkDirPath)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	currentSize := totalSize(fileInfos)
	flowTotalSizeInt64, err := strconv.ParseInt(flowTotalSize, 10, 64)
	if err != nil {
		h.log.Error(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	if flowTotalSizeInt64 == currentSize {
		fta := &FileToAssemble{chunkDirPath, fileName}
		h.completedFilesCh <- fta
	}
}