Example #1
0
func (u Util) GetFileRandom() (this_file types.File, err error) {
	r := hash.Rand64()
	err = u.Gfs.Find(bson.M{"random": bson.M{"$gt": r}}).One(&this_file)
	if err != nil {
		return this_file, err
	}
	if len(this_file.Md5) == 0 {
		err = u.Gfs.Find(bson.M{"random": bson.M{"$lt": r}}).One(&this_file)
	}
	if err != nil {
		return this_file, err
	}
	return this_file, nil
}
Example #2
0
/*
  GET /urlie
  POST /urlie
*/
func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		err := UrliePage(w)
		if err != nil {
			log.Printf("error: %s", err)
		}
		httplog.LogRequest(r, 200)
		return
	}

	if r.Method == "POST" {
		var (
			err             error
			stored_filename string
			local_filename  string
			useRandName     bool = false
			info            types.Info
		)

		info = types.Info{
			Ip:        r.RemoteAddr,
			Random:    hash.Rand64(),
			TimeStamp: time.Now(),
		}
		log.Println(info)

		err = r.ParseMultipartForm(1024 * 5)
		if err != nil {
			serverErr(w, r, err)
			return
		}

		log.Printf("%q", r.MultipartForm.Value)
		for k, v := range r.MultipartForm.Value {
			if k == "keywords" {
				info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
			} else if k == "url" {
				local_filename, err = util.FetchFileFromURL(v[0])
				if err != nil {
					serverErr(w, r, err)
					return
				} else if len(local_filename) == 0 {
					httplog.LogRequest(r, 404)
					http.NotFound(w, r)
					return
				}
				// Yay, hopefully we got an image!
			} else if k == "rand" {
				useRandName = true
			} else {
				log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
			}
		}
		exists, err := du.HasFileByFilename(filepath.Base(strings.ToLower(local_filename)))
		if err != nil {
			serverErr(w, r, err)
			return
		}

		if exists || useRandName {
			ext := filepath.Ext(local_filename)
			str := hash.GetSmallHash()
			stored_filename = fmt.Sprintf("%s%s", str, ext)
		} else {
			stored_filename = filepath.Base(local_filename)
		}

		file, err := gfs.Create(strings.ToLower(stored_filename))
		defer file.Close()
		if err != nil {
			serverErr(w, r, err)
			return
		}

		local_fh, err := os.Open(local_filename)
		defer local_fh.Close()
		if err != nil {
			serverErr(w, r, err)
			return
		}

		file.SetMeta(&info)

		// copy the request body into the gfs file
		n, err := io.Copy(file, local_fh)
		if err != nil {
			serverErr(w, r, err)
			return
		}
		log.Printf("Wrote [%d] bytes from %s to %s", n, local_filename, stored_filename)

		http.Redirect(w, r, fmt.Sprintf("/v/%s", stored_filename), 302)
	} else {
		httplog.LogRequest(r, 404)
		http.NotFound(w, r)
		return
	}
	httplog.LogRequest(r, 200) // if we make it this far, then log success
}
Example #3
0
/*
  GET /upload
  POST /upload
*/
func routeUpload(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		// Show the upload form
		httplog.LogRequest(r, 200) // if we make it this far, then log success
		err := UploadPage(w)
		if err != nil {
			log.Printf("error: %s", err)
		}
		return
	}

	if r.Method == "POST" {
		info := types.Info{
			Ip:        r.RemoteAddr,
			Random:    hash.Rand64(),
			TimeStamp: time.Now(),
		}

		// handle the form posting to this route
		err := r.ParseMultipartForm(1024 * 5)
		if err != nil {
			serverErr(w, r, err)
			return
		}
		useRandName := false
		log.Printf("%q", r.MultipartForm.Value)
		for k, v := range r.MultipartForm.Value {
			if k == "keywords" {
				info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
			} else if k == "rand" {
				useRandName = true
			} else {
				log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
			}
		}

		filehdr := r.MultipartForm.File["filename"][0]
		filename := filehdr.Filename
		exists, err := du.HasFileByFilename(filename)
		if err != nil {
			serverErr(w, r, err)
			return
		}
		if exists || useRandName {
			ext := filepath.Ext(filename)
			str := hash.GetSmallHash()
			filename = fmt.Sprintf("%s%s", str, ext)
		}

		file, err := gfs.Create(filename)
		defer file.Close()
		if err != nil {
			serverErr(w, r, err)
			return
		}
		file.SetMeta(&info)

		multiFile, err := filehdr.Open()
		if err != nil {
			log.Println(err)
			return
		}
		n, err := io.Copy(file, multiFile)
		if err != nil {
			serverErr(w, r, err)
			return
		}
		if n != r.ContentLength {
			log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
				filename,
				r.ContentLength,
				n)
		}

		http.Redirect(w, r, fmt.Sprintf("/v/%s", filename), 302)
	} else {
		httplog.LogRequest(r, 404)
		http.NotFound(w, r)
		return
	}
	httplog.LogRequest(r, 200) // if we make it this far, then log success
}
Example #4
0
// Create the file by the name in the path and/or parameter?
// add keywords from the parameters
// look for an image in the r.Body
func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
	uriChunks := chunkURI(r.URL.Path)
	if len(uriChunks) > 2 &&
		((len(uriChunks) == 2 && len(uriChunks[1]) == 0) &&
			len(r.URL.RawQuery) == 0) {
		httplog.LogRequest(r, 403)
		http.Error(w, "Not Acceptable", 403)
		return
	}

	var filename string
	info := types.Info{
		Ip:        r.RemoteAddr,
		Random:    hash.Rand64(),
		TimeStamp: time.Now(),
	}

	filename = r.FormValue("filename")
	if len(filename) == 0 && len(uriChunks) == 2 && len(uriChunks[1]) != 0 {
		filename = strings.ToLower(uriChunks[1])
	}
	log.Printf("%s\n", filename)

	var p_ext string
	p_ext = r.FormValue("ext")
	if len(filename) > 0 && len(p_ext) == 0 {
		p_ext = filepath.Ext(filename)
	} else if len(p_ext) > 0 && strings.HasPrefix(p_ext, ".") {
		p_ext = fmt.Sprintf(".%s", p_ext)
	}

	for _, word := range []string{
		"k", "key", "keyword",
		"keys", "keywords",
	} {
		v := r.FormValue(word)
		if len(v) > 0 {
			if strings.Contains(v, ",") {
				info.Keywords = append(info.Keywords, strings.Split(v, ",")...)
			} else {
				info.Keywords = append(info.Keywords, v)
			}
		}
	}

	if len(filename) == 0 {
		str := hash.GetSmallHash()
		if len(p_ext) == 0 {
			filename = fmt.Sprintf("%s.jpg", str)
		} else {
			filename = fmt.Sprintf("%s%s", str, p_ext)
		}
	}

	exists, err := du.HasFileByFilename(filename)
	if err == nil && !exists {
		file, err := gfs.Create(filename)
		defer file.Close()
		if err != nil {
			serverErr(w, r, err)
			return
		}

		file.SetMeta(&info)

		// copy the request body into the gfs file
		n, err := io.Copy(file, r.Body)
		if err != nil {
			serverErr(w, r, err)
			return
		}

		if n != r.ContentLength {
			log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
				filename,
				r.ContentLength,
				n)
		}
	} else if exists {
		if r.Method == "PUT" {
			// TODO nothing will get here presently. Workflow needs more review
			file, err := gfs.Open(filename)
			defer file.Close()
			if err != nil {
				serverErr(w, r, err)
				return
			}

			var mInfo types.Info
			err = file.GetMeta(&mInfo)
			if err != nil {
				log.Printf("ERROR: failed to get metadata for %s. %s\n", filename, err)
			}
			mInfo.Keywords = append(mInfo.Keywords, info.Keywords...)
			file.SetMeta(&mInfo)
		} else {
			log.Printf("[%s] already exists", filename)
		}
	} else {
		serverErr(w, r, err)
		return
	}

	if strings.Contains(r.Header.Get("Accept"), "text/html") {
		io.WriteString(w,
			fmt.Sprintf("<a href=\"/f/%s\">/f/%s</a>\n", filename, filename))
	} else {
		io.WriteString(w, fmt.Sprintf("/f/%s\n", filename))
	}

	httplog.LogRequest(r, 200)
}