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 })); }
public async Task <IActionResult> Edit(Guid id, [Bind("Id,Name,DOCreate,InDirId,CopyMove,IsShared,SharingCode")] LDir lDir) { if (id != lDir.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; var _LDir = await _context.LDir.FirstOrDefaultAsync(p => p.Id == lDir.Id && p.UserId == UserId_); if (_LDir == null) { return(NotFound()); } if (lDir.InDirId != Guid.Empty && !await _context.LDir.AnyAsync(p => p.Id == lDir.InDirId && p.UserId == UserId_)) { ModelState.AddModelError(nameof(lDir.InDirId), _localizer["Sorry! the directory can't be found"]); return(View(lDir)); } #region MOVE // Move if (lDir.CopyMove == CopyMove.Move) { _LDir.InDirId = lDir.InDirId; } #endregion #region COPY // Copy else if (lDir.CopyMove == CopyMove.Copy) { #region COPY DIRECTORY 01 var _AllUserDirs = await _context.LDir.Where(p => p.UserId == UserId_).ToListAsync(); var _AllUserFiles = await _context.LUserFile.Where(p => p.UserId == UserId_).ToListAsync(); var _ID8InDirIds = _AllUserDirs.Select(p => new ID8InDirId { InDirId = p.InDirId, Id = p.Id }) .Union(_AllUserFiles.Select(p => new ID8InDirId { Id = p.Id, InDirId = p.InDirId }).ToList()); var IncludedDirs = _AllUserDirs.Where(p => _xUserFileHelper.IsFileOrDirInDir(_ID8InDirIds, p.Id, lDir.Id)).ToList(); var IncludedFiles = _AllUserFiles.Where(p => _xUserFileHelper.IsFileOrDirInDir(_ID8InDirIds, p.Id, lDir.Id)).ToList(); var IDMap = new List <ID8NewID>(); var NewDirs = new List <LDir>(); var NewUserFiles = new List <LUserFile>(); IncludedDirs.ForEach(d => { var _d = new LDir { Id = Guid.NewGuid(), Name = d.Name, UserId = UserId_ }; NewDirs.Add(_d); IDMap.Add(new ID8NewID { OriginalId = d.Id, NewId = _d.Id, OriginalInDirId = d.InDirId }); }); // Add the main directory (Copy of lDir) var MainDir = new LDir { Id = Guid.NewGuid(), InDirId = lDir.InDirId, Name = lDir.Name, UserId = UserId_ }; NewDirs.Add(MainDir); // Add it here or UserFile copy in the root of main directory(lDir) can't find it's InDirId IDMap.Add(new ID8NewID { OriginalId = lDir.Id, NewId = MainDir.Id, OriginalInDirId = lDir.InDirId }); NewDirs.ForEach(_d => { #region // You don't need to know here maybe, because I don't know what I'm writing too // var OrginalId = IDMap.FirstOrDefault(p => p.NewId == _d.Id).Id; // var CopiedDirInDirId_ = _AllUserDirs.FirstOrDefault(p => p.Id ==OrginalId).InDirId; #endregion var _OriginalInDirId = IDMap.FirstOrDefault(p => p.NewId == _d.Id).OriginalInDirId; _d.InDirId = IDMap.FirstOrDefault(p => p.OriginalId == _OriginalInDirId)?.NewId ?? lDir.InDirId; // IDMap.FirstOrDefault(p => p.Id == CopiedDirInDirId_)?.NewId??lDir.InDirId; }); IncludedFiles.ForEach(f => { var _f = new LUserFile { Id = Guid.NewGuid(), MD5 = f.MD5, Name = f.Name, UserId = UserId_ }; NewUserFiles.Add(_f); IDMap.Add(new ID8NewID { OriginalId = f.Id, NewId = _f.Id, OriginalInDirId = f.InDirId }); }); NewUserFiles.ForEach(_f => { var _OriginalInDirId = IDMap.FirstOrDefault(p => p.NewId == _f.Id).OriginalInDirId; _f.InDirId = IDMap.FirstOrDefault(p => p.OriginalId == _OriginalInDirId).NewId; }); await _context.LDir.AddRangeAsync(NewDirs); await _context.LUserFile.AddRangeAsync(NewUserFiles); // await _context.SaveChangesAsync(); #endregion } #endregion #region RENAME ONLY else { _LDir.Name = lDir.Name; // _lUserFile.ContentType = _mimeHelper.GetMime(lUserFile.Name, _hostingEnv).Last(); // _lUserFile.ContentType = _mimeHelper.GetMime(lUserFile.Name).Last(); // new Mime().Lookup(lUserFile.Name); _context.Update(_LDir); // await _context.SaveChangesAsync(); } #endregion await _context.SaveChangesAsync(); return(RedirectToAction(actionName: "Index", controllerName: "LUserFiles", routeValues: new { lDir.InDirId })); } catch (DbUpdateConcurrencyException) { if (!LDirExists(lDir.Id)) { return(NotFound()); } else { throw; } } } return(View(lDir)); }
public async Task <IActionResult> Edit(Guid id, [Bind("Id,Name,CopyMove,InDirId,IsShared,SharingCode")] LUserFile lUserFile) { if (id != lUserFile.Id) { return(NotFound()); } if (ModelState.IsValid) { var InDirId = Guid.Empty; try { var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id; var _lUserFile = await _context.LUserFile.FirstOrDefaultAsync(p => p.Id == lUserFile.Id && p.UserId == UserId_); if (_lUserFile == null) { return(NotFound()); } if (lUserFile.InDirId != Guid.Empty && !await _context.LDir.AnyAsync(p => p.Id == lUserFile.InDirId && p.UserId == UserId_)) { ModelState.AddModelError(nameof(lUserFile.InDirId), _localizer["Sorry! the directory can't be found"]); return(View(lUserFile)); } #region COPY if (lUserFile.CopyMove == CopyMove.Copy) { await _context.LUserFile.AddAsync( new LUserFile { InDirId = lUserFile.InDirId, MD5 = _lUserFile.MD5, Name = _lUserFile.Name, UserId = UserId_, Id = Guid.NewGuid() }); await _context.SaveChangesAsync(); InDirId = lUserFile.InDirId; } #endregion #region MOVE else if (lUserFile.CopyMove == CopyMove.Move) { await _context.SaveChangesAsync(); InDirId = lUserFile.InDirId; } #endregion #region RENAME ONLY else { _lUserFile.Name = lUserFile.Name; #region // _lUserFile.ContentType = _mimeHelper.GetMime(lUserFile.Name, _hostingEnv).Last(); // _lUserFile.ContentType = _mimeHelper.GetMime(lUserFile.Name).Last(); // new Mime().Lookup(lUserFile.Name); #endregion _context.Update(_lUserFile); await _context.SaveChangesAsync(); InDirId = _lUserFile.InDirId; } #endregion } catch (DbUpdateConcurrencyException) { if (!LUserFileExists(lUserFile.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index), new { InDirId })); } return(View(lUserFile)); }