/** * 显示文章缩略图,顺序为:文章第一张图 -> 随机获取 * * @return */ public string show_thumb(Contents contents) { if (null == contents) { return(""); } Set_current_article(contents); if (StringKit.IsNotBlank(contents.ThumbImg)) { string newFileName = IBlogsUtils.getFileName(contents.ThumbImg); string thumbnailImgUrl = (contents.ThumbImg).Replace(newFileName, "thumbnail_" + newFileName); return(thumbnailImgUrl); } string content = article(contents.Content); string img = IBlogsUtils.show_thumb(content); if (StringKit.IsNotBlank(img)) { return(img); } int?cid = contents.Id; int?size = cid % 20; size = size == 0 ? 1 : size; return("/templates/themes/default/static/img/rand/" + size + ".jpg"); }
public void Delete(int id) { var attach = _repository.FirstOrDefault(id); if (attach == null) { return; } var filePath = _env.WebRootPath + attach.FKey; var newFileName = IBlogsUtils.getFileName(attach.FKey); var thumbnailFilePath = _env.WebRootPath + attach.FKey.Replace(newFileName, "thumbnail_" + newFileName); _repository.Delete(attach); _repository.SaveChanges(); File.Delete(filePath); File.Delete(thumbnailFilePath); }
public async Task <ApiResponse <List <Attachment> > > UploadAsync(List <IFormFile> files) { //log.info("UPLOAD DIR = {}", TaleUtils.UP_DIR); var users = _userService.CurrentUsers; var uid = users.Uid; List <Attachment> errorFiles = new List <Attachment>(); List <Attachment> urls = new List <Attachment>(); var fileItems = HttpContext.Request.Form.Files; if (null == fileItems || fileItems.Count == 0) { return(ApiResponse <List <Attachment> > .Fail("请选择文件上传")); } foreach (var fileItem in fileItems) { string fname = fileItem.FileName; if ((fileItem.Length / 1024) <= iBlogsConst.MAX_FILE_SIZE) { var fkey = IBlogsUtils.getFileKey(fname, _env.WebRootPath); var ftype = fileItem.ContentType.Contains("image") ? Types.IMAGE : Types.FILE; var filePath = _env.WebRootPath + fkey; using (var stream = new FileStream(filePath, FileMode.Create)) { await fileItem.CopyToAsync(stream); } if (IBlogsUtils.isImage(filePath)) { var newFileName = IBlogsUtils.getFileName(fkey); var thumbnailFilePath = _env.WebRootPath + fkey.Replace(newFileName, "thumbnail_" + newFileName); IBlogsUtils.cutCenterImage(_env.WebRootPath + fkey, thumbnailFilePath, 270, 380); } Attachment attachment = new Attachment(); attachment.FName = fname; attachment.AuthorId = uid; attachment.FKey = fkey; attachment.FType = ftype; attachment.Created = DateTime.Now.ToUnixTimestamp(); if (await _attachService.Save(attachment)) { urls.Add(attachment); } else { errorFiles.Add(attachment); } } else { Attachment attachment = new Attachment(); attachment.FName = fname; errorFiles.Add(attachment); } } if (errorFiles.Count > 0) { return(ApiResponse <List <Attachment> > .Fail().SetPayload(errorFiles)); } return(ApiResponse <List <Attachment> > .Ok(urls)); }