Пример #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,InDirId")] LDir lDir)
        {
            if (ModelState.IsValid)
            {
                var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id;
                lDir.Id     = Guid.NewGuid();
                lDir.UserId = UserId_;
                await _context.LDir.AddAsync(lDir);

                await _context.SaveChangesAsync();

                return(RedirectToAction(actionName: "Index", controllerName: "LUserFiles", routeValues: new { lDir.InDirId }));
                //  return LocalRedirect(lDir.ReturnUrl);
            }
            return(View());
        }
Пример #2
0
        // GET: LDirs/Create
        public async Task <IActionResult> Create(Guid?InDirId)
        {
            #region

            /*
             * ViewData[nameof(InDirId)]= InDirId ?? Guid.Empty;
             * var UserId_ = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id;
             * var LDirs = await _context.LDir.Where(p => p.UserId == UserId_ && p.InDirId == InDirId).ToListAsync();
             */
            #endregion
            var LDir_ = new LDir
            {
                InDirId   = InDirId ?? Guid.Empty,
                InDirName = (await _context.LDir.Where(p => p.InDirId == InDirId).Select(p => p.Name).FirstOrDefaultAsync()) ?? "root",
                //  ReturnUrl = ReturnUrl
            };
            return(View(LDir_));
        }
Пример #3
0
        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));
        }