public async Task <IActionResult> GetFile(Guid?id) { if (id == null) { return(NotFound()); } var _UserId = _userManager.GetUserAsync(User)?.GetAwaiter().GetResult()?.Id; #region /* #region EARTICLE_HOME_COVER * var _userEArticleHomeInfo = await _context.userEArticleHomeInfos.FirstOrDefaultAsync(p => p.UserId == _UserId && p.Id == id); * if (_userEArticleHomeInfo != null) * { * var _FilePath_ = X_DOVEValues.FileStoragePath(_hostingEnv) + _userEArticleHomeInfo.CoverFileMD5; * if (!System.IO.File.Exists(_FilePath_)) * { * return NoContent(); * } * var _FileBytes = await System.IO.File.ReadAllBytesAsync(_FilePath_); * return File(_FileBytes, _userEArticleHomeInfo.CoverFileContentType, $"EARTICLE_HOME_COVER_{_UserId}", true); * } #endregion */ #endregion #region EARTICLE_FILE var _EArticleFile = await _context.EArticleFiles.FirstOrDefaultAsync(p => p.Id == id); if (_EArticleFile != null) { var _FilePath_ = X_DOVEValues.FileStoragePath(_hostingEnv) + _EArticleFile.FileMD5; if (!System.IO.File.Exists(_FilePath_)) { return(NoContent()); } var _FileBytes = await System.IO.File.ReadAllBytesAsync(_FilePath_); return(File(_FileBytes, _xUserFileHelper.GetMimes(_EArticleFile.FileName, _hostingEnv).Last(), _EArticleFile.FileName, true)); } #endregion var _LUserFile = await _context.LUserFile.Select(p => new { p.MD5, p.Id, p.Name, p.UserId }) .FirstOrDefaultAsync(p => (p.Id == id && p.UserId == _UserId)); if (_LUserFile == null) { return(NotFound()); } var _FilePath = X_DOVEValues.FileStoragePath(_hostingEnv) + _LUserFile.MD5; if (!System.IO.File.Exists(_FilePath)) { return(NotFound()); } var FileBytes = await System.IO.File.ReadAllBytesAsync(_FilePath); return(File(FileBytes, _xUserFileHelper.GetMimes(_LUserFile.Name, _hostingEnv).Last(), _LUserFile.Name, true)); }
public async Task <IActionResult> UploadAvatar([FromForm] IFormFile avatarFile) { if (avatarFile.Length > 1024 * 1024) { return(Json(new { status = false, msg = "ExceedSize" })); // _localizer["Oops...., the size of avatar should be less than 1M"] } var _UserId = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; var memoryStream = new MemoryStream(); await avatarFile.CopyToAsync(memoryStream); var AvatarBytes = MakeCircleImage(memoryStream);// memoryStream.ToArray(); // var AvatarFilePath = X_DOVEValues.AvatarStoragePath(_hostingEnv) + _UserId; if (System.IO.File.Exists(AvatarFilePath)) { var AvatarFileBytes = await System.IO.File.ReadAllBytesAsync(AvatarFilePath); var _X_doveUserInfo = await _context.X_DoveUserInfos.FirstOrDefaultAsync(p => p.UserId == _UserId); if (_X_doveUserInfo != null && AvatarBytes == AvatarFileBytes) { // _X_doveUserInfo.AvatarBuffer return(Json(new { status = true })); // , X_DOVE_XSRF_TOKEN } } await System.IO.File.WriteAllBytesAsync(X_DOVEValues.AvatarStoragePath(_hostingEnv) + _UserId, AvatarBytes); // X_DOVE_XSRF_TOKEN = Guid.NewGuid().ToString(); // TempData["X_DOVE_XSRF_TOKEN"] = X_DOVE_XSRF_TOKEN; return(Json(new { status = true })); // , X_DOVE_XSRF_TOKEN }
public async Task <IActionResult> GetEArticleHomeCover(Guid?id) { if (id == null) { return(NotFound()); } var _UserId = _userManager.GetUserAsync(User)?.GetAwaiter().GetResult()?.Id; var _userEArticleHomeInfo = await _context.userEArticleHomeInfos.FirstOrDefaultAsync(p => p.UserId == _UserId && p.Id == id); var _FileBytes = new byte[0]; var _ContentType = "image/webp"; // string.Empty; if (_userEArticleHomeInfo == null || !System.IO.File.Exists(X_DOVEValues.FileStoragePath(_hostingEnv) + _userEArticleHomeInfo.CoverFileMD5)) { // return File(_FileBytes, _userEArticleHomeInfo.CoverFileContentType, $"EARTICLE_HOME_COVER_{_UserId}", true); _FileBytes = await System.IO.File.ReadAllBytesAsync(_hostingEnv.WebRootPath + "/img/earticle_home_default_img.webp"); // _ContentType = ; } else { var _FilePath_ = X_DOVEValues.FileStoragePath(_hostingEnv) + _userEArticleHomeInfo.CoverFileMD5; _FileBytes = await System.IO.File.ReadAllBytesAsync(_FilePath_); _ContentType = _userEArticleHomeInfo.CoverFileContentType; } return(File(_FileBytes, _ContentType, $"EARTICLE_HOME_COVER_{_UserId}", true)); }
public async Task <IActionResult> Create([FromForm] LFilePost _LFilePost) { // [Bind("Id,MD5,UserId,SharedCode,DOGenerating,Name,InDirId")] LUserFile lUserFile) if (_LFilePost._LFile.Length > 0) { var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; var _LUserFiles = await _context.LUserFile.Where(p => p.UserId == UserId_).ToListAsync(); if (await GetUsedSpaceAsync(_LUserFiles) >= (5 * 1024)) { return(Ok(new { Msg = "The storage space is used up" })); } var MemoryStream_ = new MemoryStream(); await _LFilePost._LFile.CopyToAsync(MemoryStream_); var FileBytes = MemoryStream_.ToArray(); var MD5_ = _xUserFileHelper.GetFileMD5(FileBytes); var FilePath = X_DOVEValues.FileStoragePath(_hostingEnv) + MD5_; // System.IO.File.Create(FilePath); await System.IO.File.WriteAllBytesAsync(FilePath, FileBytes); await _context.LFile.AddAsync(new LFile { MD5 = MD5_, UserId = UserId_ }); await _context.LUserFile.AddAsync(new LUserFile { UserId = UserId_, MD5 = MD5_, InDirId = _LFilePost.InDirId, Name = _LFilePost._LFile.FileName, // ContentType = _LFilePost._LFile.ContentType ?? "text/plain" }); await _context.SaveChangesAsync(); return(Ok(_LFilePost.Id)); } // process uploaded files // Don't rely on or trust the FileName property without validation. return(Ok()); #region // /* * if (ModelState.IsValid) * { * lUserFile.Id = Guid.NewGuid(); * _context.Add(lUserFile); * await _context.SaveChangesAsync(); * return RedirectToAction(nameof(Index)); * } */ // return Ok(); // View(lUserFile); #endregion }
public async Task <IActionResult> UploadEArticleImage([FromForm] IEnumerable <IFormFile> EArticleImages) { /// MD5 should be checked first if (EArticleImages.Count() > 0) { var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; var _data = new List <string>(); var _MD5s = await System.IO.Directory.GetFiles(X_DOVEValues.FileStoragePath(_hostingEnv)).Select(p => p.Split(new char[] { '\\', '/' }).LastOrDefault()).ToListAsync(); foreach (var img in EArticleImages) { var MemoryStream_ = new MemoryStream(); await img.CopyToAsync(MemoryStream_); var FileBytes = MemoryStream_.ToArray(); var MD5_ = _xUserFileHelper.GetFileMD5(FileBytes); var FilePath = X_DOVEValues.FileStoragePath(_hostingEnv) + MD5_; // System.IO.File.Create(FilePath); if (!_MD5s.Contains(MD5_)) { await System.IO.File.WriteAllBytesAsync(FilePath, FileBytes); await _context.LFile.AddAsync(new LFile { MD5 = MD5_, UserId = UserId_ }); } var _LUserFile = new LUserFile { UserId = UserId_, MD5 = MD5_, InDirId = LValue.PublicDirId, Name = img.FileName, IsEArticleFile = true // ContentType = _LFilePost._LFile.ContentType ?? "text/plain" }; _data.Add(Url.Action("GetEArticleImage", new { id = _LUserFile.Id })); await _context.LUserFile.AddAsync(_LUserFile); } await _context.SaveChangesAsync(); // see https://www.kancloud.cn/wangfupeng/wangeditor3/335782 return(Ok(new { errno = 0, data = _data.ToArray() })); } // process uploaded files // Don't rely on or trust the FileName property without validation. return(Ok(new { errno = 1 })); }
/// <summary> /// determine the MD5 in IEnumerableLMD5 is match the MD5 of uploaded files or not /// </summary> /// <param name="env">The IHostingEnvironment for getting FileStoragePath</param> /// <param name="lMD5s">The IEnumerableLMD5</param> /// <returns></returns> #endregion public List <LMD5> LCheckMD5(IHostingEnvironment env, List <LMD5> lMD5s) { var UploadFileMD5s = System.IO.Directory.GetFiles(X_DOVEValues.FileStoragePath(env)) .Select(p => System.IO.Path.GetFileNameWithoutExtension(p)).ToList(); foreach (var m in lMD5s) { if (UploadFileMD5s.Contains(m.FileMD5)) { m.IsUploaded = true; } } return(lMD5s); }
public IActionResult XCheckMd5([FromForm] List <string> MD5s) { var CheckResult = new Dictionary <string, bool>(); var UploadFileMD5s = System.IO.Directory.GetFiles(X_DOVEValues.FileStoragePath(_hostingEnv)) .Select(p => System.IO.Path.GetFileNameWithoutExtension(p)).ToList(); foreach (var m in MD5s) { if (UploadFileMD5s.Contains(m)) { CheckResult.Add(m, true); } CheckResult.Add(m, false); } return(Json(CheckResult.Select(p => new { MD5 = p.Key, IsUploaded = p.Value }).ToList())); }
public async Task <IActionResult> GetAvatar(string UserId = "") { var CurrentUserId = _userManager.GetUserAsync(User)?.GetAwaiter().GetResult()?.Id; // User.FindFirstValue(ClaimTypes.NameIdentifier); UserId = string.IsNullOrWhiteSpace(UserId) ? CurrentUserId : UserId; var AvatarFilePath = X_DOVEValues.AvatarStoragePath(_webHostEnv) + UserId; if (System.IO.File.Exists(AvatarFilePath)) { var AvatarBytes = await System.IO.File.ReadAllBytesAsync(AvatarFilePath); if (AvatarBytes != null || AvatarBytes.Length > 1) { return(File(AvatarBytes, "image/jpg")); } } var DefauleAvatar = await System.IO.File.ReadAllBytesAsync(_webHostEnv.WebRootPath + @"/img/article_publish_ico.webp"); return(File(DefauleAvatar, "image/webp")); }
/// <summary> /// Write the IFormFile bytes to "X_DOVEValues.FileStoragePath(_hostingEnv) + MD5_" and return the MD5 string /// </summary> /// <param name="_hostingEnv"></param> /// <param name="formFile"></param> /// <returns></returns> #endregion public async Task <string> LWriteBufferToFileAsync(IHostingEnvironment _hostingEnv, IFormFile formFile) { var MemoryStream_ = new MemoryStream(); await formFile.CopyToAsync(MemoryStream_); var FileBytes = MemoryStream_.ToArray(); var MD5_ = GetFileMD5(FileBytes); var UploadFileMD5s = System.IO.Directory.GetFiles(X_DOVEValues.FileStoragePath(_hostingEnv)) .Select(p => System.IO.Path.GetFileNameWithoutExtension(p)).ToList(); if (UploadFileMD5s.Contains(MD5_)) { return(MD5_); } var FilePath = X_DOVEValues.FileStoragePath(_hostingEnv) + MD5_; // System.IO.File.Create(FilePath); await System.IO.File.WriteAllBytesAsync(FilePath, FileBytes); return(MD5_); }
public async Task <IActionResult> GetAvatar(string UserId = "") { var CurrentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier); UserId = string.IsNullOrWhiteSpace(UserId) ? CurrentUserId : UserId; var AvatarFilePath = X_DOVEValues.AvatarStoragePath(_hostingEnv) + UserId; // var _X_doveUserInfo=await _context.X_DoveUserInfos.Where(p => p.UserId == UserId).FirstOrDefaultAsync(); if (System.IO.File.Exists(AvatarFilePath)) { var AvatarBytes = await System.IO.File.ReadAllBytesAsync(AvatarFilePath); if (AvatarBytes != null || AvatarBytes.Length > 1) { return(File(AvatarBytes, "image/jpg")); } } var DefauleAvatar = await System.IO.File.ReadAllBytesAsync(_hostingEnv.WebRootPath + @"/img/article_publish_ico.webp"); return(File(DefauleAvatar, "image/webp")); }
public async Task <IActionResult> UploadAvatar([FromForm] IFormFile avatarFile) { if (avatarFile.Length > 1024 * 1024) { return(Json(new { status = false, msg = "ExceedSize" })); } var _allowedAvatarFileExtensions = new string[] { "image/jpg", "image/jpeg", "image/png" }; var _isExtensionNeedChecking = false; if (_isExtensionNeedChecking && (!_allowedAvatarFileExtensions.Contains(avatarFile.ContentType.ToLower()))) { return(Json(new { status = false, msg = "RefuseExtension" })); } else if (!avatarFile.ContentType.StartsWith("image/")) { return(Json(new { status = false, msg = "RefuseExtension" })); } var _UserId = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; var memoryStream = new MemoryStream(); await avatarFile.CopyToAsync(memoryStream); var AvatarBytes = memoryStream.ToArray(); // MakeCircleImage(memoryStream);// var AvatarFilePath = X_DOVEValues.AvatarStoragePath(_webHostEnv) + _UserId; if (System.IO.File.Exists(AvatarFilePath)) { var AvatarFileBytes = await System.IO.File.ReadAllBytesAsync(AvatarFilePath); var _X_doveUserInfo = await _context.X_DoveUserInfos.FirstOrDefaultAsync(p => p.UserId == _UserId); if (_X_doveUserInfo != null && AvatarBytes == AvatarFileBytes) { return(Json(new { status = true })); // , X_DOVE_XSRF_TOKEN } } await System.IO.File.WriteAllBytesAsync(X_DOVEValues.AvatarStoragePath(_webHostEnv) + _UserId, AvatarBytes); return(Json(new { status = true })); }
public async Task <IActionResult> GetEArticleImage(Guid?id) { if (id == null) { return(NoContent()); } // var _UserId = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; var _LUserFile = await _context.LUserFile .FirstOrDefaultAsync(p => p.IsEArticleFile && p.IsLegal && p.Id == id); if (_LUserFile == null) { return(NoContent()); } var _FilePath = X_DOVEValues.FileStoragePath(_hostingEnv) + _LUserFile.MD5; if (!System.IO.File.Exists(_FilePath)) { return(NoContent()); } var FileBytes = await System.IO.File.ReadAllBytesAsync(_FilePath); return(File(FileBytes, _xUserFileHelper.GetMimes(_LUserFile.Name, _hostingEnv).Last(), _LUserFile.Name, true)); }
/* * [ActionName("GetSharedImages")] * public async Task<IActionResult> Index() * { * var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; * var _Extensions = _mimeHelper.GetExtensions("image", _hostingEnv); * var _LUserFiles = await _context.LUserFile.Where(p=>p.UserId==UserId_ && p.IsLegal).ToListAsync(); * _LUserFiles = _LUserFiles.Where(p => _Extensions.Contains(p.Name.Split('.').LastOrDefault())).ToList(); * return View("GetSharedImages", _LUserFiles); * } */ #region // GET: LUserFiles #endregion public async Task <IActionResult> Index(Guid?InDirId) { #region REBARBATIVE INITIALIZATION if (!Directory.Exists(X_DOVEValues.AvatarStoragePath(_hostingEnv))) { Directory.CreateDirectory(X_DOVEValues.AvatarStoragePath(_hostingEnv)); } var _FileNames = new DirectoryInfo(X_DOVEValues.FileStoragePath(_hostingEnv)).GetFiles().Select(p => p.Name); // Console.WriteLine("FileName >> "+JsonConvert.SerializeObject(_FileNames)); var _LFiles = await _context.LFile.ToListAsync(); var _LUserFiles = await _context.LUserFile.ToListAsync(); // Console.WriteLine("File >> "+JsonConvert.SerializeObject(_LFiles)); _LFiles.ForEach(f => { if (!_FileNames.Contains(f.MD5)) { _context.LFile.Remove(f); } }); _LUserFiles.ForEach(f => { if (!_FileNames.Contains(f.MD5)) { _context.LUserFile.Remove(f); } }); await _context.SaveChangesAsync(); #endregion var _InDirName = "root"; var _LastDirId = Guid.Empty; var _LDirs = new List <LDir>(); var _Path = string.Empty; InDirId = InDirId ?? Guid.Empty; var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; #region INITIAL_SIZE var _UserFiles_ = await _context.LUserFile.Where(p => p.UserId == UserId_).ToListAsync(); var UsedSpace = await GetUsedSpaceAsync(_UserFiles_); // 0.0; _UserFiles_ = _UserFiles_.Where(p => p.UserId == UserId_ && p.InDirId == InDirId && !string.IsNullOrWhiteSpace(p.MD5)) .OrderBy(p => p.DOCreate).ToList(); _UserFiles_.ForEach(p => { var _FileLength = new FileInfo(_xUserFileHelper.FileStoragePath(_hostingEnv) + p.MD5).Length; // UsedSpace += _FileLength / (1024 * 1024); // MByte p.Size = _FileLength; p.OptimizedSize = _xUserFileHelper.OptimizeSizeShow(_FileLength); }); /* * _UserFiles_.ForEach(p=> { * var _FileLength = new FileInfo(_xUserFileHelper.FileStoragePath(_hostingEnv) + p.MD5).Length; * UsedSpace += _FileLength/(1024*1024); // MByte * p.Size = _FileLength; * }); */ #endregion var LUserFileIds = _UserFiles_.Select(p => p.Id); #region PATH _InDirName = (await _context.LDir.Where(p => p.Id == InDirId).Select(p => p.Name).FirstOrDefaultAsync()) ?? _InDirName; _LastDirId = await _context.LDir.Where(p => p.Id == InDirId && p.UserId == UserId_).Select(p => p.InDirId).FirstOrDefaultAsync(); _LDirs = await _context.LDir.Where(p => p.UserId == UserId_ && p.InDirId == InDirId && !LUserFileIds.Contains(p.Id)) .ToListAsync(); _Path = _x_DOVEHelper.GetInPathOfFileOrDir(context: _context, InDirId: InDirId ?? Guid.Empty); // (await _context.LDir.Where(p => p.Id == InDirId).Select(p => p.Name).FirstOrDefaultAsync()) ?? "root"; // InDirId == Guid.Empty ? InDirId // : await _context.LDir.Where(p => p.Id == InDirId && p.UserId == UserId_).Select(p => p.InDirId).FirstOrDefaultAsync(); #endregion #region VIEWDATA ViewData[nameof(_Path)] = _Path; ViewData[nameof(_LDirs)] = _LDirs; ViewData[nameof(_LastDirId)] = _LastDirId; ViewData[nameof(_InDirName)] = _InDirName; ViewData[nameof(InDirId)] = InDirId; ViewData["UsedSpace"] = UsedSpace; ViewData["OptimizedUsedSpace"] = _xUserFileHelper.OptimizeSizeShow(UsedSpace); #endregion return(View(_UserFiles_)); }