Esempio n. 1
1
// Copy form file to Blob
func upload(f *multipart.FileHeader) (b *blob.Blob, err error) {
	// Open
	upload, err := f.Open()
	if err != nil {
		return
	}
	defer upload.Close()
	// Create blob
	b = blob.New()
	// Set filename from request
	b.Name = f.Filename
	// Set content-type from request
	if ct := f.Header.Get("Content-Type"); ct != "" {
		b.ContentType = ct
	}
	// Guess content-type from extension if missing
	if b.ContentType == "" || b.ContentType == ApplicationOctetStream {
		if ext := filepath.Ext(b.Name); ext != "" {
			b.ContentType = mime.TypeByExtension(ext)
		}
	}
	if b.ContentType == "" {
		b.ContentType = ApplicationOctetStream
	}
	// Write
	err = b.WriteFrom(upload)
	if err != nil {
		return
	}
	return
}
Esempio n. 2
0
func (p *FileResource) ContentTypesProvided(req Request, cxt Context) ([]MediaTypeHandler, Request, Context, int, os.Error) {
	frc := cxt.(FileResourceContext)
	var arr []MediaTypeHandler
	if frc.IsDir() {
		arr = []MediaTypeHandler{NewJsonDirectoryListing(frc.FullPath(), req.URL().Path), NewHtmlDirectoryListing(frc.FullPath(), req.URL().Path)}
	} else if frc.HasMultipleResources() {
		dir, _ := path.Split(frc.FullPath())
		filenames := frc.MultipleResourceNames()
		arr = make([]MediaTypeHandler, len(filenames))
		for i, filename := range filenames {
			extension := filepath.Ext(filename)
			mediaType := mime.TypeByExtension(extension)
			if len(mediaType) == 0 {
				// default to text/plain
				mediaType = MIME_TYPE_TEXT_PLAIN
			}
			fullFilename := path.Join(dir, filename)
			tempFrc := NewFileResourceContextWithPath(fullFilename)
			arr[i] = NewPassThroughMediaTypeHandler(mediaType, tempFrc, tempFrc.Len(), tempFrc.LastModified())
		}
	} else {
		extension := filepath.Ext(frc.FullPath())
		mediaType := mime.TypeByExtension(extension)
		if len(mediaType) == 0 {
			// default to text/plain
			mediaType = MIME_TYPE_TEXT_PLAIN
		}
		arr = []MediaTypeHandler{NewPassThroughMediaTypeHandler(mediaType, frc, frc.Len(), frc.LastModified())}
	}
	return arr, req, cxt, 0, nil
}
Esempio n. 3
0
func (s *Sisho) walkRepo() error {
	// * walk repo and generate constitution of repo.
	err := filepath.Walk(s.tmpDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}

		isGitDir, isGitDirErr := regexp.MatchString(s.tmpDir+"/.git/", path)
		if isGitDirErr != nil {
			return err
		}
		if isGitDir {
			return nil
		}

		tmp := strings.Split(path, "/")
		name := tmp[len(tmp)-1]
		ext := filepath.Ext(path)
		r := regexp.MustCompile(s.tmpDir + "/")

		if util.IsBinary(ext) {
			a := ContentAsset{
				Content: Content{
					path:         path,
					relativePath: r.ReplaceAllString(path, ""),
					name:         name,
					contentType:  mime.TypeByExtension(ext),
				},
				ext: ext,
			}
			s.assets = append(s.assets, a)
			return nil
		}

		c := Code{
			Content: Content{
				path:         path,
				relativePath: r.ReplaceAllString(path, ""),
				name:         name,
				contentType:  mime.TypeByExtension(ext),
			},
			Title:     "",
			TextLines: []string{},
		}
		s.contents = append(s.contents, c)
		return nil
	})

	if err == nil {
		return err
	}
	return nil
}
Esempio n. 4
0
//设置ContentType
func (r Response) SetContentType(ext string) {
	var contentType string
	//判断传入的扩展名是否有.
	if !strings.HasPrefix(ext, ".") {
		ext = "." + ext
	}
	//如果能从系统mime中获取相应的type,则使用系统提供的mimetype;否则的话,将application/ext直接设置为content type
	if mime.TypeByExtension(ext) != "" {
		contentType = mime.TypeByExtension(ext)
	} else {
		contentType = "application/" + strings.TrimPrefix(ext, ".") + ";charset=utf-8"
	}
	r.SetHeader("Content-Type", contentType, true)
}
Esempio n. 5
0
func fileExtImpliesText(ext string) (yes, unknown bool) {
	defer func() {
		glog.V(2).Infof("'%s' -> yes=%v   unknown=%v", ext, yes, unknown)
	}()

	if ext == "" {
		unknown = true
		return
	}
	mt := mime.TypeByExtension(ext)
	if strings.HasPrefix(mt, "text/") ||
		strings.HasSuffix(mt, "+xml") ||
		strings.HasSuffix(mt, ".json") ||
		strings.HasSuffix(mt, "+json") {
		// Most likely text.
		yes = true
		glog.V(1).Infof("Most likely a text extension: %s", ext)
		return
	}
	if strings.HasPrefix(mt, "audio/") ||
		strings.HasPrefix(mt, "image/") ||
		strings.HasPrefix(mt, "video/") {
		// Almost certainly not text.
		glog.V(1).Infof("Most likely a binary extension: %s", ext)
		return
	}
	unknown = true
	return
}
Esempio n. 6
0
// Slower operations to fill props struct
func (p *props) load(h hash.Hash, name string) *props {
	p.mime = mime.TypeByExtension(p.ext)
	r, err := os.Open(name)
	if err != nil {
		log.Print(name, ": Props: ", err)
		return p
	}
	defer r.Close()
	p.ftype = mapType(p.mime)
	// TODO: this is quite unreadable
	copy(p.chash[:], filehash(name, h, r))
	copy(p.dident[:], strhash(p.dir, h))
	// If the extension is empty, we need to detect
	// the MIME type via file contents
	if p.mime == "" {
		p.mime = sniffMIME(name, r)
	}
	// Non-images are completely processed at this point
	if !strings.HasPrefix(p.mime, "image/") {
		return p
	}
	// Image-specific processing
	if _, err := r.Seek(0, 0); err != nil {
		log.Print(name, ": Seek: ", err)
		return p
	}
	imgconf, _, err := image.DecodeConfig(r)
	if err != nil {
		log.Print(name, ": Image decoder: ", err)
		return p
	}
	p.isize = image.Point{imgconf.Width, imgconf.Height}
	return p
}
Esempio n. 7
0
func (w *messageWriter) addFiles(files []*file, isAttachment bool) {
	for _, f := range files {
		if _, ok := f.Header["Content-Type"]; !ok {
			mediaType := mime.TypeByExtension(filepath.Ext(f.Name))
			if mediaType == "" {
				mediaType = "application/octet-stream"
			}
			f.setHeader("Content-Type", mediaType+`; name="`+f.Name+`"`)
		}

		if _, ok := f.Header["Content-Transfer-Encoding"]; !ok {
			f.setHeader("Content-Transfer-Encoding", string(Base64))
		}

		if _, ok := f.Header["Content-Disposition"]; !ok {
			var disp string
			if isAttachment {
				disp = "attachment"
			} else {
				disp = "inline"
			}
			f.setHeader("Content-Disposition", disp+`; filename="`+f.Name+`"`)
		}

		if !isAttachment {
			if _, ok := f.Header["Content-ID"]; !ok {
				f.setHeader("Content-ID", "<"+f.Name+">")
			}
		}
		w.writeHeaders(f.Header)
		w.writeBody(f.CopyFunc, Base64)
	}
}
Esempio n. 8
0
func embeddedStatic() func(http.ResponseWriter, *http.Request, *log.Logger) {
	var modt = time.Now().UTC().Format(http.TimeFormat)

	return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
		file := req.URL.Path

		if file[0] == '/' {
			file = file[1:]
		}

		bs, ok := auto.Assets[file]
		if !ok {
			return
		}

		mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
		if len(mtype) != 0 {
			res.Header().Set("Content-Type", mtype)
		}
		res.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
		res.Header().Set("Last-Modified", modt)

		res.Write(bs)
	}
}
Esempio n. 9
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. 10
0
// Returns a guess at the mime type from the extension
func MimeType(o Object) string {
	mimeType := mime.TypeByExtension(path.Ext(o.Remote()))
	if mimeType == "" {
		mimeType = "application/octet-stream"
	}
	return mimeType
}
Esempio n. 11
0
func (ctx *Context) ContentType(ext string) {
	ctype := mime.TypeByExtension(ext)

	if ctype != "" {
		ctx.ResponseWriter.Header().Set("Content-Type", ctype)
	}
}
Esempio n. 12
0
File: gui.go Progetto: kat-co/juju
// serveCombo serves the GUI JavaScript and CSS files, dynamically combined.
func (h *guiHandler) serveCombo(w http.ResponseWriter, req *http.Request) {
	ctype := ""
	// The combo query is like /combo/?path/to/file1&path/to/file2 ...
	parts := strings.Split(req.URL.RawQuery, "&")
	paths := make([]string, 0, len(parts))
	for _, p := range parts {
		fpath, err := getGUIComboPath(h.rootDir, p)
		if err != nil {
			sendError(w, errors.Annotate(err, "cannot combine files"))
			return
		}
		if fpath == "" {
			continue
		}
		paths = append(paths, fpath)
		// Assume the Juju GUI does not mix different content types when
		// combining contents.
		if ctype == "" {
			ctype = mime.TypeByExtension(filepath.Ext(fpath))
		}
	}
	w.Header().Set("Content-Type", ctype)
	for _, fpath := range paths {
		sendGUIComboFile(w, fpath)
	}
}
Esempio n. 13
0
// Deploys a site to S3.
func (s *Site) Deploy(user, pass, url string) error {
	auth := aws.Auth{AccessKey: user, SecretKey: pass}
	b := s3.New(auth, aws.USEast).Bucket(url)

	// walks _site directory and uploads file to S3
	walker := func(fn string, fi os.FileInfo, err error) error {
		if fi.IsDir() {
			return nil
		}

		rel, _ := filepath.Rel(s.Dest, fn)
		typ := mime.TypeByExtension(filepath.Ext(rel))
		content, err := ioutil.ReadFile(fn)
		log.Printf(MsgUploadFile, rel)
		if err != nil {
			return err
		}

		// try to upload the file ... sometimes this fails due to amazon
		// issues. If so, we'll re-try
		if err := b.Put(rel, content, typ, s3.PublicRead); err != nil {
			time.Sleep(100 * time.Millisecond) // sleep so that we don't immediately retry
			return b.Put(rel, content, typ, s3.PublicRead)
		}

		// file upload was a success, return nil
		return nil
	}

	return filepath.Walk(s.Dest, walker)
}
Esempio n. 14
0
File: s3.go Progetto: Luzifer/s3sync
func (s *s3Provider) WriteFile(path string, content io.ReadSeeker, public bool) error {
	bucket, path, err := s.getBucketPath(path)
	if err != nil {
		return err
	}

	ext := filepath.Ext(path)
	mimeType := mime.TypeByExtension(ext)
	if mimeType == "" {
		mimeType = "application/octet-stream"
	}

	params := &s3.PutObjectInput{
		Bucket:      aws.String(bucket),
		Key:         aws.String(path),
		Body:        content,
		ContentType: aws.String(mimeType),
	}
	if public {
		params.ACL = aws.String("public-read")
	}
	_, err = s.conn.PutObject(params)

	return err
}
Esempio n. 15
0
//File saving routines
func saveFile(header *multipart.FileHeader, r io.Reader) (string, error) {
	if client, err := storage.GetNewStorageClient(); err == nil {
		h := public.NewHashString()
		objName := storage.PathJoin(storage.APPLICATIONS_FOLDER_NAME, h)
		//Determine the extension
		var ext string = ""
		if header != nil {
			if segs := strings.Split(header.Filename, "."); len(segs) > 1 {
				ext = "." + segs[len(segs)-1]
				objName = (objName + ext)
			}
		}

		obj := client.GetDefaultBucket().Object(objName)
		objWriter := obj.NewWriter(client.Ctx)

		_, err = io.Copy(objWriter, r)
		objWriter.Close()
		if err == nil {

			if attr, e := obj.Attrs(client.Ctx); attr != nil && e == nil {
				if mimeStr := mime.TypeByExtension(ext); len(mimeStr) > 0 {
					attr.ContentType = mimeStr
					obj.Update(client.Ctx, *attr)
				}
			}

			return objName, nil
		} else {
			return "", err
		}
	} else {
		return "", err
	}
}
Esempio n. 16
0
func walkpath(path string, f os.FileInfo, err error) error {
	fi, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := fi.Close(); err != nil {
			panic(err)
		}
	}()

	ext := filepath.Ext(path)
	// This tests if something is a directory; basically, look for a file extension. If that doesn't exist, assume
	// it's a directory and don't upload it to S3
	if len(ext) > 0 {
		key := path[2:len(path)]

		mtype := mime.TypeByExtension(ext)
		fmt.Println(key)

		err = s3Upload(&key, &bucket, fi, &mtype)
		if err != nil {
			panic(err)
		}
	} else {
		fmt.Println("Directory, skipping")
	}

	return err
}
Esempio n. 17
0
func makeVisitor(uploads chan FileUpload, bucket *s3.Bucket, waiter *sync.WaitGroup, args args) func(string, os.FileInfo, error) error {
	return func(fpath string, f os.FileInfo, err error) error {
		node := isfile(f)
		if node {
			contType := mime.TypeByExtension(path.Ext(fpath))
			if contType == "" {
				contType = args.mimetype
			}
			fu := FileUpload{
				ContentType: contType,
				Path:        fpath,
				Bucket:      bucket,
			}
			if runtime.NumGoroutine() > concurrency {
				uploadFile(fu, args, nil)
			} else {
				waiter.Add(1)
				go uploadFile(fu, args, func() {
					waiter.Done()
				})
			}
		}
		return nil
	}
}
Esempio n. 18
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. 19
0
// FileServerHandler function
func FileServerHandler(c *echo.Context) error {
	fileType := mime.TypeByExtension(filepath.Ext(filepath.Base(c.Request().URL.String())))
	log.Println(c.Request().URL.String()[1:])
	c.Response().Header().Set("Content-Type", fileType)
	io.Copy(c.Response(), bytes.NewReader(staticFiles.Get(c.Request().URL.String()[1:])))
	return nil
}
Esempio n. 20
0
func (v *Views) ServeHTTP(w http.ResponseWriter, r *http.Request, ss sessions.Session) {
	if v.Rewrite(w, r, ss) {
		// redirected
		return
	}
	var ok bool
	name := r.URL.Path[1:]
	if _, ok = v.Names[name]; !ok {
		name = v.Index
	}

	ext := path.Ext(name)
	ct := mime.TypeByExtension(ext)
	w.Header().Set("Content-Type", ct)

	data, _ := Asset(name)
	hdr := r.Header.Get("Accept-Encoding")
	if strings.Contains(hdr, "gzip") {
		w.Header().Set("Content-Encoding", "gzip")
		w.Write(data)
	} else {
		gz, err := gzip.NewReader(bytes.NewBuffer(data))
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		io.Copy(w, gz)
		gz.Close()
	}
}
Esempio n. 21
0
func newFilePart(fullPathFilename string) (ret FilePart, err error) {
	fh, openErr := os.Open(fullPathFilename)
	if openErr != nil {
		glog.V(0).Info("Failed to open file: ", fullPathFilename)
		return ret, openErr
	}
	ret.Reader = fh

	if fi, fiErr := fh.Stat(); fiErr != nil {
		glog.V(0).Info("Failed to stat file:", fullPathFilename)
		return ret, fiErr
	} else {
		ret.ModTime = fi.ModTime().UTC().Unix()
		ret.FileSize = fi.Size()
	}
	ext := strings.ToLower(path.Ext(fullPathFilename))
	ret.IsGzipped = ext == ".gz"
	if ret.IsGzipped {
		ret.FileName = fullPathFilename[0 : len(fullPathFilename)-3]
	}
	ret.FileName = fullPathFilename
	if ext != "" {
		ret.MimeType = mime.TypeByExtension(ext)
	}

	return ret, nil
}
Esempio n. 22
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. 23
0
// UploadReleaseAsset creates an asset by uploading a file into a release repository.
// To upload assets that cannot be represented by an os.File, call NewUploadRequest directly.
//
// GitHub API docs : http://developer.github.com/v3/repos/releases/#upload-a-release-asset
func (s *RepositoriesService) UploadReleaseAsset(owner, repo string, id int, opt *UploadOptions, file *os.File) (*ReleaseAsset, *Response, error) {
	u := fmt.Sprintf("repos/%s/%s/releases/%d/assets", owner, repo, id)
	u, err := addOptions(u, opt)
	if err != nil {
		return nil, nil, err
	}

	stat, err := file.Stat()
	if err != nil {
		return nil, nil, err
	}
	if stat.IsDir() {
		return nil, nil, errors.New("the asset to upload can't be a directory")
	}

	mediaType := mime.TypeByExtension(filepath.Ext(file.Name()))
	req, err := s.client.NewUploadRequest(u, file, stat.Size(), mediaType)
	if err != nil {
		return nil, nil, err
	}

	asset := new(ReleaseAsset)
	resp, err := s.client.Do(req, asset)
	if err != nil {
		return nil, resp, err
	}
	return asset, resp, err
}
Esempio n. 24
0
File: file.go Progetto: toksea/iris
// TypeByExtension returns the MIME type associated with the file extension ext.
// The extension ext should begin with a leading dot, as in ".html".
// When ext has no associated type, TypeByExtension returns "".
//
// Extensions are looked up first case-sensitively, then case-insensitively.
//
// The built-in table is small but on unix it is augmented by the local
// system's mime.types file(s) if available under one or more of these
// names:
//
//   /etc/mime.types
//   /etc/apache2/mime.types
//   /etc/apache/mime.types
//
// On Windows, MIME types are extracted from the registry.
//
// Text types have the charset parameter set to "utf-8" by default.
func TypeByExtension(fullfilename string) (t string) {
	ext := filepath.Ext(fullfilename)
	//these should be found by the windows(registry) and unix(apache) but on windows some machines have problems on this part.
	if t = mime.TypeByExtension(ext); t == "" {
		// no use of map here because we will have to lock/unlock it, by hand is better, no problem:
		if ext == ".json" {
			t = "application/json"
		} else if ext == ".zip" {
			t = "application/zip"
		} else if ext == ".3gp" {
			t = "video/3gpp"
		} else if ext == ".7z" {
			t = "application/x-7z-compressed"
		} else if ext == ".ace" {
			t = "application/x-ace-compressed"
		} else if ext == ".aac" {
			t = "audio/x-aac"
		} else if ext == ".ico" { // for any case
			t = "image/x-icon"
		} else {
			t = ContentBINARY
		}
	}
	return
}
Esempio n. 25
0
func (statics *AssestStruct) FileHandlerFunc(name string) http.HandlerFunc {
	if strings.Contains(name, "private") {
		return http.NotFound
	}
	static, err := statics.GetAssestFile(name)
	return func(w http.ResponseWriter, r *http.Request) {
		if err != nil {
			http.NotFound(w, r)
			return
		}
		modtime := time.Unix(static.Mtime, 0)
		modifiedSince := r.Header.Get("If-Modified-Since")
		if modifiedSince != "" {
			t, err := time.Parse(http.TimeFormat, modifiedSince)
			if err == nil && modtime.Before(t.Add(1*time.Second)) {
				w.Header().Del("Content-Type")
				w.Header().Del("Content-Length")
				w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
				w.WriteHeader(http.StatusNotModified)
				return
			}
		}

		mimeType := mime.TypeByExtension(filepath.Ext(static.Name))
		if mimeType != "" {
			w.Header().Set("Content-Type", mimeType)
		}
		w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
		w.Write([]byte(static.Content))
	}
}
Esempio n. 26
0
// Guess image mime types from gif/jpeg/png/webp
func guessImageMimeTypes(r io.Reader) string {
	format, err := guessImageFormat(r)
	if format == "" || err != nil {
		return ""
	}
	return mime.TypeByExtension("." + format)
}
Esempio n. 27
0
func GetInfoForBytes(filename string, data []byte) (*FileInfo, *AppError) {
	size := len(data)

	var mimeType string
	extension := filepath.Ext(filename)
	isImage := IsFileExtImage(extension)
	if isImage {
		mimeType = GetImageMimeType(extension)
	} else {
		mimeType = mime.TypeByExtension(extension)
	}

	hasPreviewImage := isImage
	if mimeType == "image/gif" {
		// just show the gif itself instead of a preview image for animated gifs
		if gifImage, err := gif.DecodeAll(bytes.NewReader(data)); err != nil {
			return nil, NewLocAppError("GetInfoForBytes", "model.file_info.get.gif.app_error", nil, "filename="+filename)
		} else {
			hasPreviewImage = len(gifImage.Image) == 1
		}
	}

	return &FileInfo{
		Filename:        filename,
		Size:            size,
		Extension:       extension[1:],
		MimeType:        mimeType,
		HasPreviewImage: hasPreviewImage,
	}, nil
}
Esempio n. 28
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. 29
0
func (s embeddedStatic) mimeTypeForFile(file string) string {
	// We use a built in table of the common types since the system
	// TypeByExtension might be unreliable. But if we don't know, we delegate
	// to the system.
	ext := filepath.Ext(file)
	switch ext {
	case ".htm", ".html":
		return "text/html"
	case ".css":
		return "text/css"
	case ".js":
		return "application/javascript"
	case ".json":
		return "application/json"
	case ".png":
		return "image/png"
	case ".ttf":
		return "application/x-font-ttf"
	case ".woff":
		return "application/x-font-woff"
	case ".svg":
		return "image/svg+xml"
	default:
		return mime.TypeByExtension(ext)
	}
}
Esempio n. 30
0
// @router /test/file/download [get]
func (c *TestController) Download() {
	filename := c.GetString("name")
	if filename == "" {
		http.Error(c.Ctx.ResponseWriter, "Not Found", 404)
		return
	}
	filename = fileDir + filename
	_, err := os.Stat(filename)
	if os.IsNotExist(err) {
		http.Error(c.Ctx.ResponseWriter, "Not Found", 404)
		return
	}

	ctype := mime.TypeByExtension(filepath.Ext(filename))
	if ctype == "" {
		ctype = "application/oct-stream"
	}
	en := context.ParseEncoding(c.Ctx.Request)
	if en != "" {
		c.Ctx.ResponseWriter.Header().Set("Content-Encoding", en)
	}
	c.Ctx.ResponseWriter.Header().Set("Content-Type", ctype)

	f, err := os.Open(filename)
	context.WriteFile(en, c.Ctx.ResponseWriter, f)
}