public async Task <IActionResult> GetImage(string fileId) { //try //{ // if (string.IsNullOrEmpty(fileId)) return Content("error"); // var myFileId = fileId.ToLongByHex(); // var noteFile = noteFileService.GetFile(myFileId); // if (noteFile == null) // //return Content("NoFoundImage"); // return NoFoundImage(); // //获取又拍云操作对象 // UpYun upyun = new UpYun(config.UpyunConfig.UpyunBucket, config.UpyunConfig.UpyunUsername, config.UpyunConfig.UpyunPassword); // upyun.secret = config.UpyunConfig.UpyunSecret; // string path = noteFile.Path; // int unixTimestamp = UnixTimeHelper.GetTimeStampInInt32(); // unixTimestamp += 15; // string _upt = upyun.CreatToken(unixTimestamp.ToString(), upyun.secret, path); // return Redirect($"https://upyun.morenote.top{path}?_upt={_upt}"); //} //catch (Exception ex) //{ // return NoFoundImage(); //} try { if (string.IsNullOrEmpty(fileId)) { return(Content("error")); } var myFileId = fileId.ToLongByHex(); var noteFile = noteFileService.GetFile(myFileId); if (noteFile == null) { //return Content("NoFoundImage"); return(NoFoundImage()); } //获取操作对象 string fileExt = Path.GetExtension(noteFile.Name); var fileService = FileStoreServiceFactory.Instance(webSiteConfig); var objectName = $"{noteFile.UserId.ToHex()}/images/{noteFile.CreatedTime.ToString("yyyy")}/{noteFile.CreatedTime.ToString("MM")}/{noteFile.FileId.ToHex()}{Path.GetExtension(noteFile.Name)}"; var provider = new FileExtensionContentTypeProvider(); var memi = provider.Mappings[fileExt]; var data = await fileService.GetObjecByteArraytAsync(webSiteConfig.MinIOConfig.NoteFileBucketName, objectName); return(File(data, memi)); } catch (Exception ex) { return(NoFoundImage()); } }
//下载附件 public async Task <IActionResult> Download(string attachId) { //todo:bug 要使用流式下载,减少下载时候的内存消耗 var attach = await attachService.GetAttachAsync(attachId.ToLongByHex(), GetUserIdBySession()); var provider = new FileExtensionContentTypeProvider(); string fileExt = Path.GetExtension(attach.Name); var memi = provider.Mappings[fileExt]; var fileService = FileStoreServiceFactory.Instance(config); var data = await fileService.GetObjecByteArraytAsync(config.MinIOConfig.NoteFileBucketName, attach.Path); return(File(data, memi, attach.Title)); }
//todo: 考虑该函数的删除文件的安全性,是否存在注入的风险 private async Task DeleteAttachOnDiskAsync(string path) { try { var fileStore = FileStoreServiceFactory.Instance(config); await fileStore.RemoveObjectAsync(config.MinIOConfig.NoteFileBucketName, path); } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); throw; } }
public async Task <bool> SaveFile(string objectName, IFormFile formFile, string contentType) { try { var fileStore = FileStoreServiceFactory.Instance(webSiteConfig); using (Stream stream = formFile.OpenReadStream()) { await fileStore.PutObjectAsync(webSiteConfig.MinIOConfig.NoteFileBucketName, objectName, stream, stream.Length, contentType); } return(true); } catch (Exception ex) { return(false); } }
public async Task <IActionResult> GetAvatar(string userIdHex, string filename) { var fileUrlPath = $"{userIdHex}/images/logo"; var objectName = $"{fileUrlPath}/{filename}"; var provider = new FileExtensionContentTypeProvider(); string fileExt = Path.GetExtension(filename); var memi = provider.Mappings[fileExt]; var fileService = FileStoreServiceFactory.Instance(webSiteConfig); try { var data = await fileService.GetObjecByteArraytAsync(webSiteConfig.MinIOConfig.NoteFileBucketName, objectName); return(File(data, memi)); } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Content("NotFound")); } }
//下载全部的附件get all attachs by noteId public async Task <IActionResult> DownloadAll(string noteId) { var note = noteService.GetNoteById(noteId.ToLongByHex()); if (note == null) { return(Content("No found note,Please check the noteId")); } if (GetUserIdBySession() != note.UserId) { return(Content("No permission to access attachments")); } // 得到文件列表 var attachs = await attachService.ListAttachsAsync(noteId.ToLongByHex(), GetUserIdBySession()); if (attachs.IsNullOrNothing()) { return(Content("")); } var sb = new StringBuilder(); var fileStore = FileStoreServiceFactory.Instance(config); //下载拼接 foreach (var attach in attachs) { sb.Append(attach.AttachId); } //计算AttachId合并字符串的哈希 string md5 = SHAEncryptHelper.MD5Encrypt(sb.ToString()); var dir = config.FileStoreConfig.TempFolder + Path.DirectorySeparatorChar + note.NoteId.ToHex(); var zipFileName = config.FileStoreConfig.TempFolder + Path.DirectorySeparatorChar + md5 + ".zip"; if (!System.IO.File.Exists(zipFileName)) { //清理文件夹 if (Directory.Exists(dir)) { Directory.Delete(dir, true); } Directory.CreateDirectory(dir); //下载附件到本地 foreach (var attach in attachs) { string fileName = TextFilterUtil.DelUnSafeChar(attach.Title); await fileStore.GetObjectAsync(config.MinIOConfig.NoteFileBucketName, attach.Path, dir + Path.DirectorySeparatorChar + fileName); } //执行压缩 ZipHelper compressedFilesHelper = new ZipHelper(); compressedFilesHelper.CreateZipFile(zipFileName, null, dir); } var memi = GetMemi(".zip"); var stream = System.IO.File.Open(zipFileName, FileMode.Open, FileAccess.Read); { return(File(stream, memi, Path.GetFileName(zipFileName))); } }