Esempio n. 1
0
func (c *appContext) uploadPic(a *multipart.FileHeader) (string, string) {
	log.Println("In upload pic territory")

	bucket := c.bucket

	file, err := a.Open()
	defer file.Close()
	if err != nil {
		panic(err.Error())
	}

	if err != nil {
		panic(err)
	}

	buf, _ := ioutil.ReadAll(file)

	fn := uuid.New()
	fname := "places/" + fn + path.Ext(a.Filename)
	thumbname := "placesthumb/" + fn + path.Ext(a.Filename)
	log.Println(fname)

	b := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + fname
	d := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + thumbname

	filetype := http.DetectContentType(buf)

	err = bucket.Put(fname, buf, filetype, s3.PublicRead)

	if err != nil {
		log.Println("bucket put error for main image")
		panic(err.Error())

	}
	log.Print("added a full image")
	img, err := jpeg.Decode(bytes.NewReader(buf))

	if err != nil {
		log.Println(err.Error())
	}

	m := resize.Resize(200, 200, img, resize.Lanczos2)

	buf2 := new(bytes.Buffer)
	err = jpeg.Encode(buf2, m, nil)
	if err != nil {
		fmt.Println(err.Error())
	}
	thumb := buf2.Bytes()
	filetype2 := http.DetectContentType(thumb)
	err = bucket.Put(thumbname, thumb, filetype2, s3.PublicRead)

	if err != nil {
		log.Println("bucket put error for thumbnail")
		panic(err.Error())
	}
	log.Println("uploaded one thumb image")
	return b, d

}
Esempio n. 2
0
File: api.go Progetto: aimless/g0
func (a *Api) GetThumbnail(w rest.ResponseWriter, r *rest.Request) {
	name := r.PathParam("name")
	exists, data := a.binthumb.Retrieve(name)
	if exists {
		w.Header().Add("Content-Type", http.DetectContentType(data))
		w.(http.ResponseWriter).Write(data)
	} else {
		_, data = a.binimage.Retrieve(name)
		datatype := http.DetectContentType(data)
		outbuf := bytes.NewBuffer([]byte{})

		if datatype == "video/webm" {
			//v := nutil.GetVideoFrame(bytes.NewBuffer(data))
			//i, _, _ = image.Decode(v)
			v := nutil.GetGifThumbnail(bytes.NewBuffer(data))
			outbuf.ReadFrom(v)

		} else {
			i, _, _ := image.Decode(bytes.NewReader(data))
			thmb := nutil.MakePropThumbnail(i, 314, 0)
			jpeg.Encode(outbuf, thmb, nil)
		}

		a.binthumb.Insert(name, outbuf.Bytes())
		w.Header().Add("Content-Type", http.DetectContentType(outbuf.Bytes()))
		w.(http.ResponseWriter).Write(outbuf.Bytes())
	}

	return
}
Esempio n. 3
0
func mainHandler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, error) {
			return []byte(serectkey), nil
		})
		if err == nil && token.Valid {
			log.Println("Executing mainHandler")
			log.Println("Content-Length:", r.ContentLength)
			if r.ContentLength == 0 {
				w.Header().Set("xxx", "xxx")
				log.Println(http.StatusText(400))
				http.Error(w, http.StatusText(400), 400)
				return
			}
			buf := new(bytes.Buffer)
			buf.ReadFrom(r.Body)
			d := buf.Bytes()
			log.Println("Content-Type:", http.DetectContentType(d))
			//log.Println(string(d[:33]))
			if http.DetectContentType(buf.Bytes()) != "text/plain; charset=utf-8" {
				w.Header().Set("xxx", "xxx")
				http.Error(w, http.StatusText(415), 415)
				return
			}
			h.ServeHTTP(w, r)
			log.Println("Executing mainHandler again")
		} else {
			log.Println("Invalid Token!")
			return
		}
	})
}
Esempio n. 4
0
//判断文件协议content-type,0表示xml,1表示
//alter:    ,by:   ,time:  ,#第几次修改
func DecideFile(body []byte) int {
	if http.DetectContentType(body) == "text/xml; charset=utf-8" {
		return 0
	} else if http.DetectContentType(body) == "text/plain; charset=utf-8" {
		return 1
	}
	return 3
}
Esempio n. 5
0
// PutObject uploads a Google Cloud Storage object.
// shouldRetry will be true if the put failed due to authorization, but
// credentials have been refreshed and another attempt is likely to succeed.
// In this case, content will have been consumed.
func (gsa *Client) PutObject(obj *Object, content io.Reader) error {
	if err := obj.valid(); err != nil {
		return err
	}
	const maxSlurp = 2 << 20
	var buf bytes.Buffer
	n, err := io.CopyN(&buf, content, maxSlurp)
	if err != nil && err != io.EOF {
		return err
	}
	contentType := http.DetectContentType(buf.Bytes())
	if contentType == "application/octet-stream" && n < maxSlurp && utf8.Valid(buf.Bytes()) {
		contentType = "text/plain; charset=utf-8"
	}

	objURL := gsAccessURL + "/" + obj.Bucket + "/" + obj.Key
	var req *http.Request
	if req, err = http.NewRequest("PUT", objURL, ioutil.NopCloser(io.MultiReader(&buf, content))); err != nil {
		return err
	}
	req.Header.Set("x-goog-api-version", "2")
	req.Header.Set("Content-Type", contentType)

	var resp *http.Response
	if resp, err = gsa.client.Do(req); err != nil {
		return err
	}

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("Bad put response code: %v", resp.Status)
	}
	return nil
}
Esempio n. 6
0
func (cache *FileCache) HttpWriteFile(w http.ResponseWriter, r *http.Request) {
	path, err := url.QueryUnescape(r.URL.String())
	if err != nil {
		http.ServeFile(w, r, r.URL.Path)
	} else if len(path) > 1 {
		path = path[1:len(path)]
	} else {
		http.ServeFile(w, r, ".")
		return
	}

	if cache.InCache(path) {
		itm := cache.items[path]
		ctype := http.DetectContentType(itm.Access())
		mtype := mime.TypeByExtension(filepath.Ext(path))
		if mtype != "" && mtype != ctype {
			ctype = mtype
		}
		w.Header().Set("content-length", fmt.Sprintf("%d", itm.Size))
		w.Header().Set("content-disposition",
			fmt.Sprintf("filename=%s", filepath.Base(path)))
		w.Header().Set("content-type", ctype)
		w.Write(itm.Access())
		return
	}
	go cache.Cache(path)
	http.ServeFile(w, r, path)
}
Esempio n. 7
0
func (this *FileService) GetFileBase64(userId, fileId string) (str string, mine string) {
	defer func() { // 必须要先声明defer,否则不能捕获到panic异常
		if err := recover(); err != nil {
			fmt.Println(err) // 这里的err其实就是panic传入的内容,55
		}
	}()

	path := this.GetFile(userId, fileId)

	if path == "" {
		return "", ""
	}

	path = revel.BasePath + "/" + strings.TrimLeft(path, "/")

	ff, err := ioutil.ReadFile(path)
	if err != nil {
		return "", ""
	}

	e64 := base64.StdEncoding
	maxEncLen := e64.EncodedLen(len(ff))
	encBuf := make([]byte, maxEncLen)

	e64.Encode(encBuf, ff)

	mime := http.DetectContentType(ff)

	str = string(encBuf)
	return str, mime
}
Esempio n. 8
0
func (s *staticDB) Put(file *os.File, size int) error {
	s.lock.Lock()
	defer s.lock.Unlock()

	if _, ok := s.assets[file.Name()]; ok {
		return fmt.Errorf("file already known, %q", file.Name())
	}

	buf := bytes.NewBuffer(nil)

	_, err := io.Copy(buf, file)
	if err != nil {
		return err
	}

	data := buf.Bytes()

	mimetype := mime.TypeByExtension(filepath.Ext(file.Name()))
	if mimetype == "" {
		log.Printf("[INFO] Couldn't detect mimetype from exntension, sniffing content: %q", file.Name())
		mimetype = http.DetectContentType(data)
	}
	log.Printf("[INFO] Mimetype of %q: %q", file.Name(), mimetype)

	h := md5.New()
	_, _ = h.Write(data)

	s.assets[file.Name()] = &asset{
		md5hex:   hex.EncodeToString(h.Sum(nil)),
		content:  data,
		mimetype: mimetype,
	}

	return nil
}
Esempio n. 9
0
func (r *ResponseDetails) ConvertToResponseDetailsView() views.ResponseDetailsView {
	needsEncoding := false

	// Check headers for gzip
	contentEncodingValues := r.Headers["Content-Encoding"]
	if len(contentEncodingValues) > 0 {
		needsEncoding = true
	} else {
		mimeType := http.DetectContentType([]byte(r.Body))
		needsEncoding = true
		for _, v := range supportedMimeTypes {
			if strings.Contains(mimeType, v) {
				needsEncoding = false
				break
			}
		}
	}

	// If contains gzip, base64 encode
	body := r.Body
	if needsEncoding {
		body = base64.StdEncoding.EncodeToString([]byte(r.Body))
	}

	return views.ResponseDetailsView{Status: r.Status, Body: body, Headers: r.Headers, EncodedBody: needsEncoding}
}
Esempio n. 10
0
// Helper function to change the avatar
func changeAvatar(s *discordgo.Session) {

	resp, err := http.Get(URL)
	if err != nil {
		fmt.Println("Error retrieving the file, ", err)
		return
	}

	defer func() {
		_ = resp.Body.Close()
	}()

	img, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading the response, ", err)
		return
	}

	base64 := base64.StdEncoding.EncodeToString(img)

	avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), base64)

	_, err = s.UserUpdate("", "", BotUsername, avatar, "")
	if err != nil {
		fmt.Println("Error setting the avatar, ", err)
	}

}
Esempio n. 11
0
func writeCloudStorageObject(httpClient *http.Client) {
	content := os.Stdin
	const maxSlurp = 1 << 20
	var buf bytes.Buffer
	n, err := io.CopyN(&buf, content, maxSlurp)
	if err != nil && err != io.EOF {
		log.Fatalf("Error reading from stdin: %v, %v", n, err)
	}
	contentType := http.DetectContentType(buf.Bytes())

	req, err := http.NewRequest("PUT", "https://storage.googleapis.com/"+*writeObject, io.MultiReader(&buf, content))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("x-goog-api-version", "2")
	req.Header.Set("x-goog-acl", "public-read")
	req.Header.Set("Content-Type", contentType)
	res, err := httpClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	if res.StatusCode != 200 {
		res.Write(os.Stderr)
		log.Fatalf("Failed.")
	}
	log.Printf("Success.")
	os.Exit(0)
}
func (w *EncodedResponseWriter) sniff(bytes []byte) {
	if w.sniffDone {
		return
	}
	w.sniffDone = true
	// Check the content type, sniffing the initial data if necessary:
	respType := w.Header().Get("Content-Type")
	if respType == "" && bytes != nil {
		respType = http.DetectContentType(bytes)
		w.Header().Set("Content-Type", respType)
	}

	// Can/should we compress the response?
	if w.status >= 300 || w.Header().Get("Content-Encoding") != "" ||
		(!strings.HasPrefix(respType, "application/json") && !strings.HasPrefix(respType, "text/")) {
		return
	}

	// OK, we can compress the response:
	//base.LogTo("HTTP+", "GZip-compressing response")
	w.Header().Set("Content-Encoding", "gzip")
	w.Header().Del("Content-Length") // length is unknown due to compression

	// Get a gzip writer from the cache, or create a new one if it's empty:
	select {
	case w.gz = <-zipperCache:
		w.gz.Reset(w.ResponseWriter)
	default:
		w.gz = gzip.NewWriter(w.ResponseWriter)
	}
}
Esempio n. 13
0
func (w gzipResponseWriter) Write(b []byte) (int, error) {
	if "" == w.Header().Get("Content-Type") {
		// If no content type, apply sniffing algorithm to un-gzipped body.
		w.Header().Set("Content-Type", http.DetectContentType(b))
	}
	return w.Writer.Write(b)
}
Esempio n. 14
0
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
	if !w.sniffDone && w.Header().Get("Content-Type") == "" {
		w.Header().Set("Content-Type", http.DetectContentType(b))
		w.sniffDone = true
	}
	return w.gzipWriter.Write(b)
}
Esempio n. 15
0
//Upload object by its remote path and local file path. The format of remote path is "/bucketName/objectName".
func (c *Client) PutObject(opath string, filepath string) (err error) {
	if strings.HasPrefix(opath, "/") == false {
		opath = "/" + opath
	}

	//reqUrl := "http://" + c.Host + opath
	buffer := new(bytes.Buffer)

	fh, err := os.Open(filepath)
	if err != nil {
		return
	}
	defer fh.Close()
	io.Copy(buffer, fh)

	contentType := http.DetectContentType(buffer.Bytes())
	params := map[string]string{}
	params["Content-Type"] = contentType

	resp, err := c.doRequest("PUT", opath, opath, params, buffer)
	if err != nil {
		return
	}

	body, _ := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		err = errors.New(resp.Status)
		fmt.Println(string(body))
		return
	}
	return
}
Esempio n. 16
0
func Handler(m message.Message) string {
	if m.Type != "PRIVMSG" {
		return ""
	}

	url := httpRe.FindString(m.Contents)
	if url == "" {
		return ""
	}

	resp, err := http.Get(url)
	if err != nil {
		return ""
	}
	defer resp.Body.Close()

	buf := make([]byte, 4096)
	_, err = io.ReadFull(resp.Body, buf)

	mtype := http.DetectContentType(buf)
	if len(mtype) < 9 || mtype[0:9] != "text/html" {
		return ""
	}

	matches := titleRe.FindStringSubmatch(string(buf))
	if len(matches) < 2 {
		return ""
	}

	title := html.UnescapeString(matches[1])

	return "Site Title :: " + title
}
Esempio n. 17
0
func (u *Uploader) Do(f *File) error {
	urls := fmt.Sprintf("http://storage.googleapis.com/%s/%s", f.Bucket, f.Path)
	params := make(url.Values)

	urls += "?" + params.Encode()

	req, err := http.NewRequest("PUT", urls, nil)
	if err != nil {
		return err
	}

	req.Header.Set("User-Agent", "google-api-go-client/0.5")
	req.Header.Set("Content-Type", http.DetectContentType(f.Object))
	req.Header.Set("x-goog-project-id", u.ProjectId)

	body := ioutil.NopCloser(bytes.NewReader(f.Object))
	req.Body = body
	req.ContentLength = int64(len(f.Object))

	resp, err := u.client.Do(req)
	if err != nil {
		return err
	}

	defer resp.Body.Close()

	return nil
}
Esempio n. 18
0
func (fs *FileService) SendToS3(key string, fileName string) {
	file, err := os.Open(fileName)
	if err != nil {
		log.Fatal("Error opening ", fileName, ": ", err)
	}

	bucketName := os.Getenv("S3_BUCKET_NAME")

	svc := s3.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})
	fileInfo, _ := file.Stat()
	var size int64 = fileInfo.Size()
	buffer := make([]byte, size)
	file.Read(buffer)
	fileBytes := bytes.NewReader(buffer)
	fileType := http.DetectContentType(buffer)
	params := &s3.PutObjectInput{
		Bucket:        aws.String(bucketName),
		Key:           aws.String(key),
		ACL:           aws.String("public-read"),
		Body:          fileBytes,
		ContentLength: &size,
		ContentType:   aws.String(fileType),
		Metadata: map[string]*string{
			"Key": aws.String("MetadataValue"),
		},
	}

	svc.PutObject(params)
}
Esempio n. 19
0
func (this *RequestContext) Static(version string) error { // host static files
	var mimetype string
	lastdot := strings.LastIndex(this.path, ".")
	if lastdot > -1 {
		mimetype = mime.TypeByExtension(this.path[lastdot:])
	}
	w := *this.res

	var content []byte
	var err error

	if len(version) > 0 {
		content, err = getFileOfVersion(this.path, version)
	} else {
		content, err = ioutil.ReadFile(this.path)
	}
	if mimetype == "" {
		if len(content) == 0 {
			mimetype = "text/plain"
		} else {
			mimetype = http.DetectContentType(content)
		}
	}

	if err != nil {
		return err
	}
	w.Header().Set("Content-Type", mimetype)
	w.Write(content)

	return nil
}
Esempio n. 20
0
// Write wraps the underlying Write method to do compression.
func (w gzipResponseWriter) Write(b []byte) (int, error) {
	if w.Header().Get("Content-Type") == "" {
		w.Header().Set("Content-Type", http.DetectContentType(b))
	}
	n, err := w.Writer.Write(b)
	return n, err
}
Esempio n. 21
0
func DetectContentType(data []byte) string {
	ct := http.DetectContentType(data)
	if i := strings.Index(ct, ";"); i >= 0 {
		ct = ct[0:i]
	}
	return ct
}
Esempio n. 22
0
func handleImageRequest(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
	}

	w.Header().Set("Cache-Control", "public, max-age=31536000")

	// Client is checking for a cached URI, assume it is valid
	// and return a 304
	if r.Header.Get("If-Modified-Since") != "" {
		w.WriteHeader(http.StatusNotModified)
		return
	}

	gc := fetch.RequestContext(r)

	var data []byte
	err := cache.Get(gc, gc.CacheKey(), groupcache.AllocatingByteSliceSink(&data))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	w.Header().Set("Content-Type", http.DetectContentType(data))
	http.ServeContent(w, r, gc.ImageId, time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), bytes.NewReader(data))
}
Esempio n. 23
0
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := route.Context(r)

	name := strings.Trim(route.Param(ctx, "filepath"), "/")
	if name == "" {
		name = "index.html"
	}

	file, err := GetFile(StaticFiles, name)
	if err != nil {
		if err != io.EOF {
			log.Warn("Could not get file: ", err)
		}
		w.WriteHeader(http.StatusNotFound)
		return
	}
	contentType := http.DetectContentType(file)
	if strings.Contains(contentType, "text/plain") || strings.Contains(contentType, "application/octet-stream") {
		parts := strings.Split(name, ".")
		contentType = mimeMap[parts[len(parts)-1]]
	}
	w.Header().Set("Content-Type", contentType)
	w.Header().Set("Cache-Control", "public, max-age=259200")
	w.Write(file)
}
Esempio n. 24
0
// Write appends data to the gzip writer.
func (w GzipResponseWriter) Write(b []byte) (int, error) {
	if _, ok := w.Header()["Content-Type"]; !ok {
		// If content type is not set, infer it from the uncompressed body.
		w.Header().Set("Content-Type", http.DetectContentType(b))
	}
	return w.gw.Write(b)
}
Esempio n. 25
0
File: fs.go Progetto: cnhup/httpmq
func (h *fsHandler) newFSFile(f *os.File, fileInfo os.FileInfo, compressed bool) (*fsFile, error) {
	n := fileInfo.Size()
	contentLength := int(n)
	if n != int64(contentLength) {
		f.Close()
		return nil, fmt.Errorf("too big file: %d bytes", n)
	}

	// detect content-type
	ext := fileExtension(fileInfo.Name(), compressed)
	contentType := mime.TypeByExtension(ext)
	if len(contentType) == 0 {
		data, err := readFileHeader(f, compressed)
		if err != nil {
			return nil, fmt.Errorf("cannot read header of the file %q: %s", f.Name(), err)
		}
		contentType = http.DetectContentType(data)
	}

	lastModified := fileInfo.ModTime()
	ff := &fsFile{
		h:               h,
		f:               f,
		contentType:     contentType,
		contentLength:   contentLength,
		compressed:      compressed,
		lastModified:    lastModified,
		lastModifiedStr: AppendHTTPDate(nil, lastModified),

		t: time.Now(),
	}
	return ff, nil
}
Esempio n. 26
0
func (grw gzipResponseWriter) Write(p []byte) (int, error) {
	if len(grw.Header().Get(HeaderContentType)) == 0 {
		grw.Header().Set(HeaderContentType, http.DetectContentType(p))
	}

	return grw.w.Write(p)
}
Esempio n. 27
0
func IsPDFFile(data []byte) (string, bool) {
	contentType := http.DetectContentType(data)
	if strings.Index(contentType, "application/pdf") != -1 {
		return contentType, true
	}
	return contentType, false
}
Esempio n. 28
0
func imageController(w http.ResponseWriter, r *http.Request, buf []byte, Operation Operation) {
	if len(buf) == 0 {
		ErrorReply(w, ErrEmptyPayload)
		return
	}

	mimeType := http.DetectContentType(buf)
	if IsImageMimeTypeSupported(mimeType) == false {
		ErrorReply(w, ErrUnsupportedMedia)
		return
	}

	opts := readParams(r)
	if opts.Type != "" && ImageType(opts.Type) == 0 {
		ErrorReply(w, ErrOutputFormat)
		return
	}

	image, err := Operation.Run(buf, opts)
	if err != nil {
		ErrorReply(w, NewError("Error while processing the image: "+err.Error(), BAD_REQUEST))
		return
	}

	w.Header().Set("Content-Type", image.Mime)
	w.Write(image.Body)
}
Esempio n. 29
0
func IsImageFile(data []byte) (string, bool) {
	contentType := http.DetectContentType(data)
	if strings.Index(contentType, "image/") != -1 {
		return contentType, true
	}
	return contentType, false
}
Esempio n. 30
0
func (b *Build) AttachFile(f multipart.File) error {

	data, err := ioutil.ReadAll(f)
	if err != nil {
		return errors.New(errors.Error{
			Label: "internal_error",
			Field: "file",
			Text:  err.Error(),
		})
	}

	if contentType := http.DetectContentType(data); contentType != "application/zip" {
		return errors.New(errors.Error{
			Label: "internal_error",
			Field: "file",
			Text:  "Bad content-type, want application/zip have " + contentType,
		})
	}

	if b.FilePath, err = file.Store("builds", b.Id.Hex()+".zip", data); err != nil {
		return err
	}

	// Check ident
	if err := b.readRuntimeConfig(data); err != nil {
		return err
	}

	return nil
}