コード例 #1
0
ファイル: webgui.go プロジェクト: postfix/libblockify
func (h *handler) testUrl(dls *downloadStatus) {
	block1 := blockutil.AllocateBlock()
	block2 := blockutil.AllocateBlock()
	m := new(generalutils.Metrik)
	if generalutils.MeterDownloadStream(block1, block2, h.B, dls.tuple, h.Wants, m) {
		dls.curstat = m.String()
		dls.status = Complete
	} else {
		dls.curstat = m.String()
		dls.status = Uncomplete
	}
}
コード例 #2
0
ファイル: webgui.go プロジェクト: postfix/libblockify
func (h *handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	path := req.URL.Path
	switch {
	case path == "/":
		h.m.RLock()
		defer h.m.RUnlock()
		tmainview.Execute(resp, h)
		return
	case path == "/add":
		// u := req.URL.RawQuery
		if req.ParseForm() != nil {
			goto redirect
		}
		u := req.Form.Get("url")
		tuple, rest, name, e := link.ParseURL(u)
		if e != nil {
			goto redirect
		}
		h.m.Lock()
		defer h.m.Unlock()
		_, ok := h.InUse[u] // do not overwrite anything!
		if !ok {
			h.InUse[u] = &downloadStatus{decodedUrl{tuple, rest, name}, None, ""}
		}
		goto redirect
	case path == "/test":
		u, e := urllib.QueryUnescape(req.URL.RawQuery)
		if e != nil {
			goto redirect
		}
		h.m.Lock()
		defer h.m.Unlock()
		obj, ok := h.InUse[u]
		if ok && obj.status != Testing {
			obj.status = Testing
			go h.testUrl(obj)
		}
		goto redirect
	case len(path) >= len(download) && path[:len(download)] == download:
		u, e := urllib.QueryUnescape(req.URL.RawQuery)
		if e != nil {
			goto errresp
		}
		h.m.Lock()
		obj, ok := h.InUse[u]
		h.m.Unlock()
		if !ok {
			goto errresp
		}
		// resp.Header().Add("Content-Type","application/octet-stream")
		block1 := blockutil.AllocateBlock()
		block2 := blockutil.AllocateBlock()
		generalutils.DownloadStream(block1, block2, resp, h.B, obj.tuple, obj.rest)
	case path == "/upload":
		// u := req.URL.RawQuery
		if req.ParseForm() != nil {
			goto redirect
		}
		fname := req.Form.Get("path")
		src, e := os.Open(fname)
		if e != nil {
			tuploaded.Execute(resp, &upload{Error: fmt.Sprint(e)})
			return
		}
		defer src.Close()
		block1 := blockutil.AllocateBlock()
		block2 := blockutil.AllocateBlock()
		_, rest, tuple, e := generalutils.UploadStream(block1, block2, rand.Reader, src, h.B, 3, 3)
		if e != nil {
			tuploaded.Execute(resp, &upload{Error: fmt.Sprint(e)})
			return
		}
		// if broken { tuploaded.Execute(resp,&upload{Error:"Upload is broken"}) ; return }
		_, filepart := filepath.Split(fname)
		url := link.MakeURL(tuple, rest, filepart)
		tuploaded.Execute(resp, &upload{Url: url})
	}
	return
redirect:
	resp.Header().Add("Location", "/")
	resp.WriteHeader(303)
	return
errresp:
	resp.WriteHeader(404)
}