コード例 #1
0
ファイル: imgsrv.go プロジェクト: kissthink/imgsrv
func main() {
	flag.Parse()
	for _, arg := range flag.Args() {
		// TODO What to do with these floating args ...
		//      Assume they're files and upload them?
		log.Printf("%s", arg)
	}

	// loads either default or flag specified config
	// to override variables
	if c, err := config.ReadConfigFile(ConfigFile); err == nil {
		DefaultConfig.Merge(c)
	}

	if DefaultConfig.Server {
		// Run the server!

		runServer(DefaultConfig)

	} else if len(FetchUrl) > 0 {
		// not sure that this ought to be exposed in the client tool

		file, err := util.FetchFileFromURL(FetchUrl)
		if err != nil {
			log.Println(err)
			return
		}
		log.Println(file)

	} else {
		// we're pushing up a file

		if len(DefaultConfig.RemoteHost) == 0 {
			log.Println("Please provide a remotehost!")
			return
		}
		if len(PutFile) == 0 { //&& len(flag.Args()) == 0) {
			log.Println("Please provide files to be uploaded!")
			return
		}
		params := map[string]string{}
		if len(FileKeywords) > 0 {
			params["keywords"] = FileKeywords
		} else {
			log.Println("WARN: you didn't provide any keywords :-(")
		}
		url, err := url.Parse(DefaultConfig.RemoteHost + "/f/")
		if err != nil {
			log.Println(err)
			return
		}
		url_path, err := client.PutFileFromPath(url.String(), PutFile, params)
		if err != nil {
			log.Println(err)
			return
		}
		fmt.Printf("%s%s\n", DefaultConfig.RemoteHost, url_path)
	}
}
コード例 #2
0
ファイル: server.go プロジェクト: jbowes/imgsrv
/*
  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
}