Beispiel #1
0
func uploadContent(w http.ResponseWriter, r *http.Request) {
	if session.GetEmail(r) == "" {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	headers, err := prepareMultipartForm(r)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	} else if headers == nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	name := r.FormValue("name")
	if name == "" || videoExists(name) {
		http.Redirect(w, r, "/edit", http.StatusNotModified)
		return
	}

	if err = saveMediaFile(headers[0], name); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
	}

	http.Redirect(w, r, "/edit", http.StatusFound)
}
Beispiel #2
0
func deleteContent(w http.ResponseWriter, r *http.Request) {
	if session.GetEmail(r) == "" {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	typ := r.FormValue("type")
	if typ == "all" {
		if err := deleteCurrentFiles(); err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			log.Println(err)
		}
		return
	}

	fileinfos, err := ioutil.ReadDir("content")
	if err != nil {
		log.Fatal(err)
	}

	name := r.FormValue("name")
	for _, fileinfo := range fileinfos {
		filename := fileinfo.Name()
		matching := name == filenameWithoutExtension(filename)
		if matching {
			os.Remove("content/" + filename)
		}
	}

	w.WriteHeader(http.StatusOK)
}
Beispiel #3
0
func uploadLink(w http.ResponseWriter, r *http.Request) {
	if session.GetEmail(r) == "" {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	name := r.FormValue("name")
	if name == "" || videoExists(name) {
		http.Redirect(w, r, "/edit", http.StatusNotModified)
		return
	}

	// Check if there's an entry that has same name
	entries := listEntries()
	for _, entry := range entries {
		if name == entry.Name {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
	}

	url := r.FormValue("url")

	// Check if the url is valid
	matched, err := regexp.Match(`(.*youtube\.com\/.+)|(.*vimeo\.com\/.+)`, []byte(url))
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	} else if !matched {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Create the 'content' directory if doesn't exist
	if err = prepareContentDirectory(); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}

	// Create URL file
	filepath := "content/" + name + ".txt"
	output, err := os.OpenFile(filepath, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		os.Remove(filepath)
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	defer output.Close()

	// Write URL to file
	output.Write([]byte(url))

	http.Redirect(w, r, "/edit", http.StatusFound)
}
Beispiel #4
0
func login(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		if session.GetEmail(r) != "" {
			w.WriteHeader(http.StatusOK)
		} else {
			w.WriteHeader(http.StatusNotFound)
		}
	case "POST":
		authCode := r.FormValue("authCode")
		conf.RedirectURL = *redirectURL

		tok, err := conf.Exchange(oauth2.NoContext, authCode)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		client := conf.Client(oauth2.NoContext, tok)
		service, err := plus.New(client)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		call := service.People.Get("me")
		person, err := call.Do()
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		email := ""
		for _, em := range person.Emails {
			if em.Type == "account" {
				email = em.Value
				break
			}
		}

		if !strings.HasSuffix(email, "@bartleboglehegarty.com") {
			w.WriteHeader(http.StatusForbidden)
			return
		}

		session.Set(w, r, email)
		w.WriteHeader(http.StatusOK)
	}
}
Beispiel #5
0
func (wsh editWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	email := session.GetEmail(r)
	if email == "" {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		return
	}

	c := &connection{send: make(chan []byte, 256), ws: ws, h: wsh.h}
	c.h.register <- c
	defer func() { c.h.unregister <- c }()
	go c.writer()
	c.reader()
}
Beispiel #6
0
func getContent(w http.ResponseWriter, r *http.Request) {
	data := struct {
		UsedDiskSpacePercentage string
		Entries                 []Entry
		IsLoggedIn              bool
	}{
		UsedDiskSpacePercentage: fmt.Sprintf("%.1f", float64(usedDiskSpace())/float64(totalDiskSpace)*100),
		Entries:                 listEntries(),
		IsLoggedIn:              session.GetEmail(r) != "",
	}

	if *refreshTemplates {
		if tmpls, err := template.New("t").ParseGlob("templates/*.html"); err == nil {
			templates = tmpls
		}
	}

	w.WriteHeader(http.StatusOK)
	templates.ExecuteTemplate(w, "edit", data)
}
Beispiel #7
0
func editImage(w http.ResponseWriter, r *http.Request) {
	if r.Method != "PUT" {
		return
	}

	if session.GetEmail(r) == "" {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	headers, err := prepareMultipartForm(r)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	} else if headers == nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	name := r.FormValue("name")
	typ := r.FormValue("type")
	deletePreviousFile(name, typ)
	saveMediaFile(headers[0], name)
}