public AssetViewModel GetAssetDetails(AssetViewModel assetView)
        {
            assetView.AgentName = _agentRepository.Find(null, a => a.Id == assetView.AgentId).Items?.FirstOrDefault()?.Name;
            assetView.FileName  = _serverFileRepository.Find(null, f => f.Id == assetView.FileId).Items?.FirstOrDefault()?.Name;

            return(assetView);
        }
Beispiel #2
0
        public object LocalFileStorageOperation(FileManagerDirectoryContent args)
        {
            if (args.Action == "delete" || args.Action == "rename")
            {
                if ((args.TargetPath == null) && (args.Path == ""))
                {
                    FileManagerResponse response = new FileManagerResponse();
                    response.Error = new ErrorDetails {
                        Code = "401", Message = "Restricted to modify the root folder."
                    };
                    return(this.operation.ToCamelCase(response));
                }
            }

            var          serverDrive  = GetDrive();
            var          entityId     = Guid.Parse(args.Id);
            ServerFile   serverFile   = new ServerFile();
            ServerFolder serverFolder = new ServerFolder();

            if (args.IsFile)
            {
                serverFile = serverFileRepository.GetOne(entityId);
            }
            else
            {
                serverFolder = serverFolderRepository.GetOne(entityId);
            }

            switch (args.Action)
            {
            case "read":
                //reads the file(s) or folder(s) from the given path

                //update FileAttribute (retrieval)
                var fileAttribute = fileAttributeRepository.Find(null).Items?.Where(q => q.Name == FileAttributes.RetrievalCount.ToString() &&
                                                                                    q.ServerFileId == entityId).FirstOrDefault();
                fileAttribute.AttributeValue += 1;
                fileAttributeRepository.Update(fileAttribute);

                return(this.operation.ToCamelCase(this.operation.GetFiles(args.Path, args.ShowHiddenItems)));

            case "delete":
                //deletes the selected file(s) or folder(s) from the given path
                if (args.IsFile.Equals(true))
                {
                    DeleteFile(args.Path);
                    serverDrive.StorageSizeInBytes -= args.Size;
                }
                else
                {
                    //removes the size of folder and any files from server drive property
                    var  files    = serverFileRepository.Find(null).Items?.Where(q => q.StorageFolderId == entityId);
                    long fileSize = 0;
                    foreach (var file in files)
                    {
                        fileSize += file.SizeInBytes;
                    }
                    serverDrive.StorageSizeInBytes -= args.Size - fileSize;

                    DeleteFolder(args.Path);
                }

                serverDriveRepository.Update(serverDrive);
                webhookPublisher.PublishAsync("Files.DriveUpdated", serverDrive.Id.ToString(), serverDrive.Name);

                return(this.operation.ToCamelCase(this.operation.Delete(args.Path, args.Names)));

            case "copy":
                //copies the selected file(s) or folder(s) from a path and then pastes them into a given target path
                if (args.IsFile.Equals(true))
                {
                    foreach (var file in args.UploadFiles)
                    {
                        var folderId = GetFolderId(args.Path);

                        //add serverFile entity
                        SaveServerFileViewModel viewModel = new SaveServerFileViewModel()
                        {
                            Id                  = Guid.Parse(args.Id),
                            ContentType         = file.ContentType,
                            CorrelationEntity   = "",         //TODO: update or remove
                            CorrelationEntityId = Guid.Empty, //TODO: update or remove
                            HashCode            = null,
                            SizeInBytes         = file.Length,
                            StorageFolderId     = folderId,
                            StoragePath         = args.Path,
                            StorageProvider     = "LocalFileStorage",
                            File                = file
                        };
                        SaveFile(viewModel);
                    }
                }
                else
                {
                    //add serverFolder entity
                    AddFolder(args);
                }
                AddBytesToServerDrive(serverDrive, args.Size);

                return(this.operation.ToCamelCase(this.operation.Copy(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData)));

            case "move":
                //cuts the selected file(s) or folder(s) from a path and then pastes them into a given target path
                if (args.IsFile.Equals(true))
                {
                    foreach (var file in args.UploadFiles)
                    {
                        var folderId = GetFolderId(args.Path);
                        UpdateServerFileViewModel viewModel = new UpdateServerFileViewModel()
                        {
                            ContentType         = file.ContentType,
                            CorrelationEntity   = "",         //TODO: remove or update
                            CorrelationEntityId = Guid.Empty, //TODO: remove or update
                            HashCode            = null,
                            File            = file,
                            SizeInBytes     = file.Length,
                            StorageFolderId = folderId,
                            StoragePath     = Path.Combine(args.Path, file.FileName),
                            StorageProvider = "LocalFileStorage"
                        };
                        UpdateFile(viewModel);
                    }
                }
                else
                {
                    //update ServerFolder entity
                    serverFolder.ParentFolderId = Guid.Parse(args.ParentId);
                    serverFolder.StoragePath    = args.Path;
                    serverFolderRepository.Update(serverFolder);
                    webhookPublisher.PublishAsync("Files.FolderUpdated", serverFolder.Id.ToString(), serverFolder.Name);
                }

                return(this.operation.ToCamelCase(this.operation.Move(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData)));

            case "details":
                //gets the details of the selected file(s) or folder(s)
                return(this.operation.ToCamelCase(this.operation.Details(args.Path, args.Names, args.Data)));

            case "create":
                //creates a new folder in a given path

                //add ServerFolder entity
                AddFolder(args);
                AddBytesToServerDrive(serverDrive, args.Size);

                return(this.operation.ToCamelCase(this.operation.Create(args.Path, args.Name)));

            case "search":
                //gets the list of file(s) or folder(s) from a given path based on the searched key string

                //add to retrieval count value to file attribute entity for each file
                //update FileAttribute (retrieval)
                foreach (var file in args.UploadFiles)
                {
                    fileAttribute = fileAttributeRepository.Find(null).Items?.Where(q => q.Name == FileAttributes.RetrievalCount.ToString() &&
                                                                                    q.ServerFileId == entityId).FirstOrDefault();
                    fileAttribute.AttributeValue += 1;
                    fileAttributeRepository.Update(fileAttribute);
                }

                return(this.operation.ToCamelCase(this.operation.Search(args.Path, args.SearchString, args.ShowHiddenItems, args.CaseSensitive)));

            case "rename":
                //renames a file or folder
                if (args.IsFile.Equals(true))
                {
                    //update ServerFile entity
                    foreach (var file in args.UploadFiles)
                    {
                        var folderId = GetFolderId(args.Path);
                        UpdateServerFileViewModel viewModel = new UpdateServerFileViewModel()
                        {
                            ContentType         = file.ContentType,
                            CorrelationEntity   = "",         //TODO: remove or update
                            CorrelationEntityId = Guid.Empty, //TODO: remove or update
                            HashCode            = null,
                            File            = file,
                            SizeInBytes     = file.Length,
                            StorageFolderId = folderId,
                            StoragePath     = Path.Combine(args.Path, file.FileName),
                            StorageProvider = "LocalFileStorage"
                        };
                        UpdateFile(viewModel);
                    }
                }
                else
                {
                    //update ServerFolder entity
                    serverFolder.ParentFolderId = Guid.Parse(args.ParentId);
                    serverFolder.StoragePath    = args.Path;
                    serverFolderRepository.Update(serverFolder);
                    webhookPublisher.PublishAsync("Files.FolderUpdated", serverFolder.Id.ToString(), serverFolder.Name);
                }
                return(this.operation.ToCamelCase(this.operation.Rename(args.Path, args.Name, args.NewName)));
            }
            return(null);
        }
        public PaginatedList <FileFolderViewModel> FindAllFilesFoldersView(Guid?driveId, Predicate <FileFolderViewModel> predicate = null, string sortColumn = "", OrderByDirectionType direction = OrderByDirectionType.Ascending, int skip = 0, int take = 100)
        {
            PaginatedList <FileFolderViewModel> paginatedList = new PaginatedList <FileFolderViewModel>();
            var folderItemsList = base.Find(null, j => j.IsDeleted == false && j.StorageDriveId == driveId);

            if (folderItemsList != null && folderItemsList.Items != null && folderItemsList.Items.Count > 0)
            {
                var folderItemRecord = from a in folderItemsList.Items
                                       join b in dbContext.ServerFolders on a.ParentFolderId equals b.Id into table1
                                       from b in table1.DefaultIfEmpty()
                                       join c in dbContext.ServerFolders on a.Id equals c.ParentFolderId into table2
                                       from c in table1.DefaultIfEmpty()
                                       join d in dbContext.ServerDrives on a.StorageDriveId equals d.Id into table3
                                       from d in table3.DefaultIfEmpty()
                                       select new FileFolderViewModel
                {
                    Name            = a?.Name,
                    Id              = a?.Id,
                    ContentType     = "Folder",
                    CreatedBy       = a?.CreatedBy,
                    CreatedOn       = a?.CreatedOn,
                    FullStoragePath = a?.StoragePath,
                    HasChild        = c?.ParentFolderId != null ? true : false,
                    IsFile          = false,
                    ParentId        = a?.ParentFolderId,
                    StoragePath     = b?.StoragePath != null ? b?.StoragePath : d?.Name,
                    Size            = a?.SizeInBytes,
                    StorageDriveId  = a?.StorageDriveId
                };

                if (!string.IsNullOrWhiteSpace(sortColumn))
                {
                    if (direction == OrderByDirectionType.Ascending)
                    {
                        folderItemRecord = folderItemRecord.OrderBy(j => j.GetType().GetProperty(sortColumn).GetValue(j)).ToList();
                    }
                    else if (direction == OrderByDirectionType.Descending)
                    {
                        folderItemRecord = folderItemRecord.OrderByDescending(j => j.GetType().GetProperty(sortColumn).GetValue(j)).ToList();
                    }
                }

                var fileItemsList = _serverFileRepository.Find(null, j => j.IsDeleted == false && j.ServerDriveId == driveId);

                if (fileItemsList != null && fileItemsList.Items != null && fileItemsList.Items.Count > 0)
                {
                    var fileItemRecord = from a in fileItemsList.Items
                                         join b in dbContext.ServerFolders on a.StorageFolderId equals b.Id into table1
                                         from b in table1.DefaultIfEmpty()
                                         join c in dbContext.ServerDrives on a.ServerDriveId equals c.Id into table2
                                         from c in table2.DefaultIfEmpty()
                                         select new FileFolderViewModel
                    {
                        Name            = a?.Name,
                        Id              = a?.Id,
                        ContentType     = a?.ContentType,
                        CreatedBy       = a?.CreatedBy,
                        CreatedOn       = a?.CreatedOn,
                        FullStoragePath = a?.StoragePath,
                        HasChild        = false,
                        IsFile          = true,
                        ParentId        = a?.StorageFolderId,
                        StoragePath     = b?.StoragePath != null ? b?.StoragePath : c?.Name,
                        Size            = a?.SizeInBytes,
                        StorageDriveId  = a?.ServerDriveId
                    };

                    if (!string.IsNullOrWhiteSpace(sortColumn))
                    {
                        if (direction == OrderByDirectionType.Ascending)
                        {
                            fileItemRecord = fileItemRecord.OrderBy(j => j.GetType().GetProperty(sortColumn).GetValue(j)).ToList();
                        }
                        else if (direction == OrderByDirectionType.Descending)
                        {
                            fileItemRecord = fileItemRecord.OrderByDescending(j => j.GetType().GetProperty(sortColumn).GetValue(j)).ToList();
                        }
                    }

                    List <FileFolderViewModel> filterRecord = null;
                    List <FileFolderViewModel> itemRecord   = null;
                    if (folderItemRecord != null || folderItemRecord.Any())
                    {
                        itemRecord = folderItemRecord.ToList();
                        itemRecord.AddRange(fileItemRecord);
                    }
                    else
                    {
                        itemRecord = fileItemRecord.ToList();
                    }

                    if (predicate != null)
                    {
                        filterRecord = itemRecord.ToList().FindAll(predicate);
                    }
                    else
                    {
                        filterRecord = itemRecord.ToList();
                    }

                    paginatedList.Items = filterRecord.Skip(skip).Take(take).ToList();

                    paginatedList.Completed   = fileItemsList.Completed;
                    paginatedList.Impediments = fileItemsList.Impediments;
                    paginatedList.PageNumber  = fileItemsList.PageNumber;
                    paginatedList.PageSize    = fileItemsList.PageSize;
                    paginatedList.ParentId    = fileItemsList.ParentId;
                    paginatedList.Started     = fileItemsList.Started;
                    paginatedList.TotalCount  = filterRecord?.Count;
                }
                else
                {
                    List <FileFolderViewModel> filterRecord = null;
                    if (predicate != null)
                    {
                        filterRecord = folderItemRecord.ToList().FindAll(predicate);
                    }
                    else
                    {
                        filterRecord = folderItemRecord.ToList();
                    }

                    paginatedList.Items = filterRecord.Skip(skip).Take(take).ToList();

                    paginatedList.Completed   = folderItemsList.Completed;
                    paginatedList.Impediments = folderItemsList.Impediments;
                    paginatedList.PageNumber  = folderItemsList.PageNumber;
                    paginatedList.PageSize    = folderItemsList.PageSize;
                    paginatedList.ParentId    = folderItemsList.ParentId;
                    paginatedList.Started     = folderItemsList.Started;
                    paginatedList.TotalCount  = filterRecord?.Count;
                }
            }

            return(paginatedList);
        }