예제 #1
0
파일: download.go 프로젝트: tjz101/caffbox
func (c *DownloadController) Get() {
	r := c.Content.Request
	w := c.Content.ResponseWriter

	file := r.FormValue("file")
	if file == "" {
		writeMsg(w, Response{ErrCode: CODE_NOT_FOUND, ErrMsg: MSG_NOT_FOUND})
		return
	}
	filePath := util.GetPhysicalPath(file)
	fla, _ := util.FileExists(filePath)
	if !fla {
		writeMsg(w, Response{ErrCode: CODE_NOT_FOUND, ErrMsg: MSG_NOT_FOUND})
		return
	}
	display := r.FormValue("display")
	if display == "" {
		display = filepath.Base(file)
	}

	f, err := os.Open(filePath)
	if err != nil {
		writeMsg(w, Response{ErrCode: CODE_FILE_OPEN_FAILED, ErrMsg: MSG_FILE_OPEN_FAILED})
		return
	}
	defer f.Close()
	buff, err := ioutil.ReadAll(f)
	if err != nil {
		writeMsg(w, Response{ErrCode: CODE_FILE_READ_FAILED, ErrMsg: MSG_FILE_READ_FAILED})
		return
	}
	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Set("Content-Disposition", "attachment; filename=\""+display+"\"")
	w.Write(buff)
}
예제 #2
0
파일: view.go 프로젝트: tjz101/caffbox
func thumbnail(filePath string, resize string) (string, error) {
	preFilePath := filePath
	filePath = filepath.Join(filepath.Dir(preFilePath), resize, filepath.Base(preFilePath))
	if preFilePath != filePath {
		m := &sync.Mutex{}
		m.Lock()
		defer m.Unlock()
		fla, _ := util.FileExists(filePath)
		if !fla {
			newFilePath := filepath.Dir(filePath)
			fla, _ := util.DirExists(newFilePath)
			if !fla {
				err := util.Mkdir(newFilePath)
				if err != nil {
					return preFilePath, err
				}
			}
			cmdFormat := "convert %s -resize %s %s"
			cmd := fmt.Sprintf(cmdFormat, preFilePath, resize, filePath)
			list := strings.Split(cmd, " ")
			c := exec.Command(list[0], list[1:]...)
			err := c.Run()
			if err != nil {
				return preFilePath, err
			}
			return filePath, nil
		} else {
			return filePath, nil
		}
	}
	return preFilePath, nil
}
예제 #3
0
func writeFileToPath(path string, file multipart.File, watermark string, header *multipart.FileHeader) (caffbox.Response, error) {
	if path == "" {
		path = util.PathFromTime()
	}
	physicalPath := util.GetPhysicalPath(path)

	err := util.Mkdir(physicalPath)
	if err != nil {
		return caffbox.Response{ErrCode: caffbox.CODE_DIR_CREATE_FAILED, ErrMsg: caffbox.MSG_DIR_CREATE_FAILED}, err
	}

	destFilename := header.Filename
	if caffbox.Sett.Rename {
		destFilename = util.RandomString()
		destFilename = destFilename + util.GetFileSuffix(header.Filename)
	}
	filePhysicalPath := filepath.Join(physicalPath, destFilename)
	fla, err := util.FileExists(filePhysicalPath)
	if fla && err == nil {
		newFilename, err := util.IncreaseFilename(physicalPath, destFilename)
		if newFilename == destFilename || err != nil {
			return caffbox.Response{ErrCode: caffbox.CODE_FILE_ALREADY_EXISTS, ErrMsg: caffbox.MSG_FILE_ALREADY_EXISTS}, err
		} else {
			destFilename = newFilename
		}
		filePhysicalPath = filepath.Join(physicalPath, newFilename)
	}
	destFile, err := os.OpenFile(filePhysicalPath, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		return caffbox.Response{ErrCode: caffbox.CODE_FILE_OPEN_FAILED, ErrMsg: caffbox.MSG_FILE_OPEN_FAILED}, err
	}
	defer destFile.Close()
	_, err = io.Copy(destFile, file)
	if err != nil {
		return caffbox.Response{ErrCode: caffbox.CODE_FILE_SAVE_FAILED, ErrMsg: caffbox.MSG_FILE_SAVE_FAILED}, err
	}
	if watermark == "y" && caffbox.Sett.Watermark != "" {
		cmdFormat := "composite -gravity center %s %s %s"
		cmd := fmt.Sprintf(cmdFormat, caffbox.Sett.Watermark, filePhysicalPath, filePhysicalPath)
		list := strings.Split(cmd, " ")
		c := exec.Command(list[0], list[1:]...)
		err := c.Run()
		if err != nil {
			return caffbox.Response{ErrCode: caffbox.CODE_FILE_SAVE_FAILED, ErrMsg: caffbox.MSG_FILE_SAVE_FAILED}, err
		}
	}
	absPath, _ := util.GetAbsPath(physicalPath)
	return caffbox.Response{ErrCode: caffbox.CODE_SUCCESS, ErrMsg: caffbox.MSG_SUCCESS, Data: caffbox.CaffFile{Name: destFilename, Path: absPath, OriginalName: header.Filename}}, nil
}
예제 #4
0
파일: view.go 프로젝트: tjz101/caffbox
func (c *ViewController) Get() {
	r := c.Content.Request
	w := c.Content.ResponseWriter

	file := c.Content.Params["file"]
	if file == "" {
		http.NotFound(w, r)
		return
	}
	filePath := util.GetPhysicalPath(file)
	fla, _ := util.FileExists(filePath)
	if !fla {
		http.NotFound(w, r)
		return
	}
	displayname := r.FormValue("displayname")

	if displayname == "" {
		displayname = filepath.Base(file)
	}

	suffix := filePath[strings.LastIndex(filePath, "."):]
	if util.IsPic(filePath) {
		resize, err := getResize(r)
		if err != nil && resize == "" {
			caffmux.Debug(err)
		} else {
			filePath, err = thumbnail(filePath, resize)
			if err != nil {
				caffmux.Debug(err)
			}
			caffmux.Debug(filePath)
		}
	}

	contentType := ""
	switch suffix {
	case ".css":
		contentType = "text/css"
	case ".js":
		contentType = "text/javascript"
	case ".html":
		contentType = "text/html"
	case ".txt":
		contentType = "text/plain"
	case ".jpeg":
		contentType = "image/jpeg"
	case ".jpg":
		contentType = "image/jpeg"
	case ".gif":
		contentType = "image/gif"
	case ".png":
		contentType = "image/png"
	case ".bmp":
		contentType = "image/bmp"
	case ".xml":
		contentType = "text/xml"
	case ".json":
		contentType = "application/json"
	default:
	}

	if contentType == "" {
		http.NotFound(w, r)
		return
	}
	/*
		f, err := os.Open(filePath)
		if err != nil {
			http.NotFound(w, r)
			return
		}
		defer f.Close()
		d, err := f.Stat()
		if err != nil {
			http.NotFound(w, r)
			return
		}
		buff, err := ioutil.ReadAll(f)
		if err != nil {
			http.NotFound(w, r)
			return
		}
		w.Header().Set("Content-Type", contentType)
		w.Write(buff)
		http.ServeContent(w, r, d.Name(), d.ModTime(), bytes.NewReader(buff))
	*/
	http.ServeFile(w, r, filePath)
}