示例#1
0
文件: batch.go 项目: espebra/filebin
func StartWorker(WorkQueue chan Job, backend *fs.Backend) {
	for {
		select {
		case j := <-WorkQueue:
			startTime := time.Now().UTC()

			if err := backend.GenerateThumbnail(j.Bin, j.Filename, 115, 115, true); err != nil {
				j.Log.Println(err.Error())
				break
			}

			if err := backend.GenerateThumbnail(j.Bin, j.Filename, 1140, 0, false); err != nil {
				j.Log.Println(err.Error())
				break
			}

			if j.Cfg.CacheInvalidation {
				links := backend.GenerateLinks(j.Bin, j.Filename)
				for _, l := range links {
					if err := shared.PurgeURL(l.Href, j.Log); err != nil {
						j.Log.Println(err)
					}
				}
			}

			finishTime := time.Now().UTC()
			elapsedTime := finishTime.Sub(startTime)
			j.Log.Println("Batch job completed: " + j.Bin + "/" + j.Filename + " (" + elapsedTime.String() + ")")
		}
	}
}
示例#2
0
文件: api.go 项目: espebra/filebin
func DeleteFile(w http.ResponseWriter, r *http.Request, cfg config.Configuration, ctx model.Context) {
	params := mux.Vars(r)
	bin := params["bin"]
	if err := verifyBin(bin); err != nil {
		http.Error(w, "Invalid bin", 400)
		return
	}

	filename := params["filename"]
	if err := verifyFilename(filename); err != nil {
		http.Error(w, "Invalid filename", 400)
		return
	}

	f, err := ctx.Backend.DeleteFile(bin, filename)
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Internal Server Error", 500)
		return
	}

	ctx.Metrics.Incr("total-file-delete")

	event := ctx.Events.New(ctx.RemoteAddr, []string{"file", "delete"}, bin, filename)
	defer event.Done()

	if cfg.TriggerDeleteFile != "" {
		ctx.Log.Println("Executing trigger: Delete file")
		triggerDeleteFileHandler(cfg.TriggerDeleteFile, bin, filename)
	}

	// Purging any old content
	if cfg.CacheInvalidation {
		for _, l := range f.Links {
			if err := shared.PurgeURL(l.Href, ctx.Log); err != nil {
				ctx.Log.Println(err)
			}
		}
	}

	w.Header().Set("Cache-Control", "s-maxage=0, max-age=0")
	http.Error(w, "File deleted successfully", 200)
	return
}
示例#3
0
文件: api.go 项目: espebra/filebin
func DeleteBin(w http.ResponseWriter, r *http.Request, cfg config.Configuration, ctx model.Context) {
	params := mux.Vars(r)
	bin := params["bin"]
	if err := verifyBin(bin); err != nil {
		http.Error(w, "Invalid bin", 400)
		return
	}

	b, err := ctx.Backend.DeleteBin(bin)
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Internal Server Error", 500)
		return
	}

	ctx.Metrics.Incr("total-bin-delete")

	event := ctx.Events.New(ctx.RemoteAddr, []string{"bin", "delete"}, bin, "")
	defer event.Done()

	// Purging any old content
	if cfg.CacheInvalidation {
		for _, f := range b.Files {
			for _, l := range f.Links {
				if err := shared.PurgeURL(l.Href, ctx.Log); err != nil {
					ctx.Log.Println(err)
				}
			}
		}
	}

	ctx.Log.Println("Bin deleted successfully.")
	w.Header().Set("Cache-Control", "s-maxage=0, max-age=0")
	http.Error(w, "Bin Deleted Successfully", 200)
	return

}
示例#4
0
文件: api.go 项目: espebra/filebin
func Upload(w http.ResponseWriter, r *http.Request, cfg config.Configuration, ctx model.Context) {
	r.Close = true

	bin := r.Header.Get("bin")
	if err := verifyBin(bin); err != nil {
		http.Error(w, "Invalid bin", 400)
		return
	}

	b, err := ctx.Backend.GetBinMetaData(bin)
	if err == nil {
		if b.Expired {
			http.Error(w, "This bin expired "+b.ExpiresReadable+".", 410)
			return
		}
	}

	filename := sanitizeFilename(r.Header.Get("filename"))
	if err := verifyFilename(filename); err != nil {
		http.Error(w, "Invalid filename", 400)
		return
	}

	ctx.Metrics.Incr("current-upload")
	defer ctx.Metrics.Decr("current-upload")

	event := ctx.Events.New(ctx.RemoteAddr, []string{"file", "upload"}, bin, filename)
	defer event.Done()

	if i, err := strconv.Atoi(r.Header.Get("content-length")); err == nil {
		event.Update("Size: "+humanize.Bytes(uint64(i)), 0)
	}

	if ctx.Backend.BinExists(bin) == false {
		if cfg.TriggerNewBin != "" {
			ctx.Log.Println("Executing trigger: New bin")
			triggerNewBinHandler(cfg.TriggerNewBin, bin)
		}
	}

	f, err := ctx.Backend.UploadFile(bin, filename, r.Body)
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		event.Update(err.Error(), 2)
		return
	}

	ctx.Metrics.Incr("total-upload")

	if cfg.TriggerUploadFile != "" {
		ctx.Log.Println("Executing trigger: Uploaded file")
		triggerUploadFileHandler(cfg.TriggerUploadFile, f.Bin, f.Filename)
	}

	// Purging any old content
	if cfg.CacheInvalidation {
		for _, l := range f.Links {
			if err := shared.PurgeURL(l.Href, ctx.Log); err != nil {
				ctx.Log.Println(err)
			}
		}
	}

	j := model.Job{}
	j.Filename = f.Filename
	j.Bin = f.Bin
	j.Log = ctx.Log
	j.Cfg = &cfg
	ctx.WorkQueue <- j

	w.Header().Set("Content-Type", "application/json")

	var status = 201
	output.JSONresponse(w, status, f, ctx)
}