Esempio n. 1
0
func FetchArchive(w http.ResponseWriter, r *http.Request, cfg config.Configuration, ctx model.Context) {
	params := mux.Vars(r)
	t := model.Tag{}
	err := t.SetTag(params["tag"])
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Invalid tag", 400)
		return
	}

	t.SetTagDir(cfg.Filedir)
	t.CalculateExpiration(cfg.Expiration)
	if t.TagDirExists() {
		expired, err := t.IsExpired(cfg.Expiration)
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Internal server error", 500)
			return
		}
		if expired {
			ctx.Log.Println("Expired: " + t.ExpirationReadable)
			http.Error(w, "This tag has expired.", 410)
			return
		}

		err = t.StatInfo()
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Internal Server Error", 500)
			return
		}

		err = t.List(cfg.Baseurl)
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Error reading the tag contents.", 404)
			return
		}
	} else {
		// The tag does not exist
		http.Error(w, "Not found", 404)
		return
	}

	w.Header().Set("Cache-Control", "s-maxage=3600")

	var status = 200
	w.Header().Set("Content-Type", "application/zip")

	// Generate a map of paths to add to the zip response
	var paths []string
	for _, f := range t.Files {
		path := filepath.Join(f.TagDir, f.Filename)
		paths = append(paths, path)
	}
	output.ZIPresponse(w, status, t.Tag, paths, ctx)
	return
}
Esempio n. 2
0
func FetchTag(w http.ResponseWriter, r *http.Request, cfg config.Configuration, ctx model.Context) {
	params := mux.Vars(r)
	t := model.Tag{}
	err := t.SetTag(params["tag"])
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Invalid tag", 400)
		return
	}

	t.SetTagDir(cfg.Filedir)
	t.CalculateExpiration(cfg.Expiration)
	if t.TagDirExists() {
		expired, err := t.IsExpired(cfg.Expiration)
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Internal server error", 500)
			return
		}
		if expired {
			ctx.Log.Println("Expired: " + t.ExpirationReadable)
			http.Error(w, "This tag has expired.", 410)
			return
		}

		err = t.StatInfo()
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Internal Server Error", 500)
			return
		}

		err = t.List(cfg.Baseurl)
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Error reading the tag contents.", 404)
			return
		}
	}

	w.Header().Set("Vary", "Content-Type")
	w.Header().Set("Cache-Control", "s-maxage=3600")

	var status = 200

	if r.Header.Get("Content-Type") == "application/json" {
		w.Header().Set("Content-Type", "application/json")
		output.JSONresponse(w, status, t, ctx)
		return
	} else {
		if len(t.Files) == 0 {
			output.HTMLresponse(w, "newtag", status, t, ctx)
		} else {
			output.HTMLresponse(w, "viewtag", status, t, ctx)
		}
		return
	}
}
Esempio n. 3
0
func FetchAlbum(w http.ResponseWriter, r *http.Request, cfg config.Configuration, ctx model.Context) {
	t := model.Tag{}
	params := mux.Vars(r)
	err := t.SetTag(params["tag"])
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Invalid tag", 400)
		return
	}

	t.SetTagDir(cfg.Filedir)
	t.CalculateExpiration(cfg.Expiration)
	if t.TagDirExists() {
		expired, err := t.IsExpired(cfg.Expiration)
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Internal server error", 500)
			return
		}
		if expired {
			ctx.Log.Println("Expired: " + t.ExpirationReadable)
			http.Error(w, "This tag has expired.", 410)
			return
		}

		err = t.StatInfo()
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Internal Server Error", 500)
			return
		}

		err = t.List(cfg.Baseurl)
		if err != nil {
			ctx.Log.Println(err)
			http.Error(w, "Error reading the tag contents.", 404)
			return
		}
	} else {
		// The tag does not exist
		http.Error(w, "Not found", 404)
		return
	}

	w.Header().Set("Cache-Control", "s-maxage=3600")

	var status = 200
	output.HTMLresponse(w, "viewalbum", status, t, ctx)
	return
}
Esempio n. 4
0
func DeleteTag(w http.ResponseWriter, r *http.Request, cfg config.Configuration, ctx model.Context) {
	var err error
	params := mux.Vars(r)

	t := model.Tag{}

	err = t.SetTag(params["tag"])
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Invalid tag", 400)
		return
	}
	t.SetTagDir(cfg.Filedir)

	// Tag does not exist
	if t.TagDirExists() == false {
		http.Error(w, "Tag Not Found", 404)
		return
	}

	t.List(cfg.Baseurl)
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Internal Server Error", 500)
		return
	}

	if cfg.TriggerDeleteTag != "" {
		ctx.Log.Println("Executing trigger: Delete tag")
		triggerDeleteTagHandler(cfg.TriggerDeleteTag, t.Tag)
	}

	// Tag exists, so let's remove it.
	err = t.Remove()
	if err != nil {
		ctx.Log.Println(err)
		http.Error(w, "Internal Server Error", 500)
		return
	}

	// Verify that the tag directory is removed before sending the response.
	if t.TagDirExists() == true {
		// Failsafe. This should not happen.
		ctx.Log.Println("Failed to delete the tag. The tag dir still exists.")
		http.Error(w, "Internal Server Error", 500)
		return
	}

	// Purging any old content
	if cfg.CacheInvalidation {
		for _, f := range t.Files {
			if err := f.Purge(); err != nil {
				ctx.Log.Println(err)
			}
		}
	}

	ctx.Log.Println("Tag deleted successfully.")
	http.Error(w, "Tag Deleted Successfully", 200)
	return

}