Beispiel #1
0
func (this *Upload) Build(origin *OriginFile) (*StoreFile, error) {
	filename := origin.FileName
	var name string
	arr := strings.Split(filename, ".")
	ext := ""
	if len(arr) > 1 {
		ext = strings.ToLower("." + arr[len(arr)-1])
		name = filename[0 : len(filename)-len(ext)]
	} else {
		name = filename
	}

	if !util.InStringArray(this.ExtAllowedList, ext) {
		return nil, errors.New("file " + ext + " is forbidden")
	}

	return &StoreFile{
		StorePath:  strings.TrimPrefix(this.StorePath, "/"),
		StoreName:  util.Unique(),
		Ext:        ext,
		Name:       name,
		OriginFile: origin}, nil
}
Beispiel #2
0
// check cache file, if cache file is exists, it will return content by cache and
// if cache file is not exists, it will create cache file
// filename=/content/cid?abc=123
// the dir /var/content and file /var/content/cid?abc=123 will be created.
func (p *Page) CachePage() {
	if p.Cache.Type == "file" {
		filename := "var/cache" + p.Ctx.Request.RequestURI + ".html"
		uri := []byte(p.Ctx.Request.RequestURI)
		n := bytes.Index(uri, []byte("?"))
		var t []byte

		if n != -1 {
			t = bytes.Trim(uri[:n], "/")
		} else {
			t = bytes.Trim(uri, "/")
		}
		arr := bytes.Split(t, []byte("/"))
		// prepare dir, if dir is not exists , it will be create.
		path := "var/cache/"
		count := len(arr) - 1
		for i := 0; i < count; i++ {
			path += string(arr[i]) + "/"

			if util.InStringArray(cachePathList, path) {
				continue
			} else {
				cachePathList = append(cachePathList, path)
			}

			if _, err := os.Stat(path); err != nil {
				if os.IsNotExist(err) {
					os.Mkdir(path, os.ModeDir)
				}
			}

		}

		p.savePageToFile(filename)
		http.ServeFile(p.Ctx.ResponseWriter, p.Ctx.Request, filename)
	}
}