本文整理汇总了Golang中github.com/leanote/leanote/app/info.File类的典型用法代码示例。如果您正苦于以下问题:Golang File类的具体用法?Golang File怎么用?Golang File使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了File类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CopyHttpImage
// 复制外网的图片, 成公共图片 放在/upload下
// 都要好好的计算大小
func (c File) CopyHttpImage(src string) revel.Result {
re := info.NewRe()
fileUrlPath := "upload/" + c.GetUserId() + "/images"
dir := revel.BasePath + "/public/" + fileUrlPath
err := os.MkdirAll(dir, 0755)
if err != nil {
return c.RenderJson(re)
}
filesize, filename, _, ok := netutil.WriteUrl(src, dir)
if !ok {
re.Msg = "copy error"
return c.RenderJson(re)
}
// File
fileInfo := info.File{Name: filename,
Title: filename,
Path: fileUrlPath + "/" + filename,
Size: filesize}
id := bson.NewObjectId()
fileInfo.FileId = id
re.Id = id.Hex()
re.Item = fileInfo.Path
re.Ok, re.Msg = fileService.AddImage(fileInfo, "", c.GetUserId(), true)
return c.RenderJson(re)
}
示例2: DeleteImage
// delete image
func (this *FileService) DeleteImage(userId, fileId string) (bool, string) {
file := info.File{}
db.GetByIdAndUserId(db.Files, fileId, userId, &file)
if file.FileId != "" {
if db.DeleteByIdAndUserId(db.Files, fileId, userId) {
// delete image
// TODO
file.Path = strings.TrimLeft(file.Path, "/")
var err error
if strings.HasPrefix(file.Path, "upload") {
Log(file.Path)
err = os.Remove(revel.BasePath + "/public/" + file.Path)
} else {
err = os.Remove(revel.BasePath + "/" + file.Path)
}
if err == nil {
return true, ""
}
return false, "delete file error!"
}
return false, "db error"
}
return false, "no such item"
}
示例3: CopyHttpImage
// 复制外网的图片
// 都要好好的计算大小
func (c File) CopyHttpImage(src string) revel.Result {
re := info.NewRe()
// 生成上传路径
newGuid := NewGuid()
userId := c.GetUserId()
// fileUrlPath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images"
fileUrlPath := "files/" + GetRandomFilePath(userId, newGuid) + "/images"
dir := revel.BasePath + "/" + fileUrlPath
err := os.MkdirAll(dir, 0755)
if err != nil {
return c.RenderJson(re)
}
filesize, filename, _, ok := netutil.WriteUrl(src, dir)
if !ok {
re.Msg = "copy error"
return c.RenderJson(re)
}
// File
fileInfo := info.File{Name: filename,
Title: filename,
Path: fileUrlPath + "/" + filename,
Size: filesize}
id := bson.NewObjectId()
fileInfo.FileId = id
re.Id = id.Hex()
// re.Item = fileInfo.Path
re.Ok, re.Msg = fileService.AddImage(fileInfo, "", c.GetUserId(), true)
return c.RenderJson(re)
}
示例4: AddImage
// add Image
func (this *FileService) AddImage(image info.File, albumId, userId string) bool {
image.CreatedTime = time.Now()
if albumId != "" {
image.AlbumId = bson.ObjectIdHex(albumId)
} else {
image.AlbumId = bson.ObjectIdHex(DEFAULT_ALBUM_ID)
image.IsDefaultAlbum = true
}
image.UserId = bson.ObjectIdHex(userId)
return db.Insert(db.Files, image)
}
示例5: CopyImage
// 复制共享的笔记时, 复制其中的图片到我本地
// 复制图片
func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, string) {
// 是否已经复制过了
file2 := info.File{}
db.GetByQ(db.Files, bson.M{"UserId": bson.ObjectIdHex(toUserId), "FromFileId": bson.ObjectIdHex(fileId)}, &file2)
if file2.FileId != "" {
return true, file2.FileId.Hex()
}
// 复制之
file := info.File{}
db.GetByIdAndUserId(db.Files, fileId, userId, &file)
if file.FileId == "" || file.UserId.Hex() != userId {
return false, ""
}
_, ext := SplitFilename(file.Name)
guid := NewGuid()
newFilename := guid + ext
// TODO 统一目录格式
// dir := "files/" + toUserId + "/images"
dir := "files/" + GetRandomFilePath(toUserId, guid) + "/images"
filePath := dir + "/" + newFilename
err := os.MkdirAll(revel.BasePath+dir, 0755)
if err != nil {
return false, ""
}
_, err = CopyFile(revel.BasePath+"/"+file.Path, revel.BasePath+"/"+filePath)
if err != nil {
return false, ""
}
fileInfo := info.File{Name: newFilename,
Title: file.Title,
Path: filePath,
Size: file.Size,
FromFileId: file.FileId}
id := bson.NewObjectId()
fileInfo.FileId = id
fileId = id.Hex()
Ok, _ := this.AddImage(fileInfo, "", toUserId, false)
if Ok {
return Ok, id.Hex()
}
return false, ""
}
示例6: uploadImage
// 上传图片, 公用方法
// upload image common func
func (c File) uploadImage(from, albumId string) (re info.Re) {
var fileUrlPath = ""
var fileId = ""
var resultCode = 0 // 1表示正常
var resultMsg = "error" // 错误信息
var Ok = false
defer func() {
re.Id = fileId // 只是id, 没有其它信息
re.Code = resultCode
re.Msg = resultMsg
re.Ok = Ok
}()
file, handel, err := c.Request.FormFile("file")
if err != nil {
return re
}
defer file.Close()
// 生成上传路径
if from == "logo" || from == "blogLogo" {
fileUrlPath = "public/upload/" + c.GetUserId() + "/images/logo"
} else {
fileUrlPath = "files/" + c.GetUserId() + "/images"
}
dir := revel.BasePath + "/" + fileUrlPath
err = os.MkdirAll(dir, 0755)
if err != nil {
return re
}
// 生成新的文件名
filename := handel.Filename
var ext string
if from == "pasteImage" {
ext = ".png" // TODO 可能不是png类型
} else {
_, ext = SplitFilename(filename)
if ext != ".gif" && ext != ".jpg" && ext != ".png" && ext != ".bmp" && ext != ".jpeg" {
resultMsg = "Please upload image"
return re
}
}
filename = NewGuid() + ext
data, err := ioutil.ReadAll(file)
if err != nil {
LogJ(err)
return re
}
var maxFileSize float64
if from == "logo" {
maxFileSize = configService.GetUploadSize("uploadAvatarSize")
} else if from == "blogLogo" {
maxFileSize = configService.GetUploadSize("uploadBlogLogoSize")
} else {
maxFileSize = configService.GetUploadSize("uploadImageSize")
}
if maxFileSize <= 0 {
maxFileSize = 1000
}
// > 2M?
if float64(len(data)) > maxFileSize*float64(1024*1024) {
resultCode = 0
resultMsg = fmt.Sprintf("The file Size is bigger than %vM", maxFileSize)
return re
}
toPath := dir + "/" + filename
err = ioutil.WriteFile(toPath, data, 0777)
if err != nil {
LogJ(err)
return re
}
// 改变成gif图片
_, toPathGif := TransToGif(toPath, 0, true)
filename = GetFilename(toPathGif)
filesize := GetFilesize(toPathGif)
fileUrlPath += "/" + filename
resultCode = 1
resultMsg = "Upload Success!"
// File
fileInfo := info.File{Name: filename,
Title: handel.Filename,
Path: fileUrlPath,
Size: filesize}
id := bson.NewObjectId()
fileInfo.FileId = id
fileId = id.Hex()
if from == "logo" || from == "blogLogo" {
fileId = "public/upload/" + c.GetUserId() + "/images/logo/" + filename
}
Ok, resultMsg = fileService.AddImage(fileInfo, albumId, c.GetUserId(), from == "" || from == "pasteImage")
//.........这里部分代码省略.........