Example #1
0
        /// <summary>
        /// Prepare paged backup file list model
        /// </summary>
        /// <param name="searchModel">Backup file search model</param>
        /// <returns>Backup file list model</returns>
        public virtual BackupFileListModel PrepareBackupFileListModel(BackupFileSearchModel searchModel)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            //get backup files
            var backupFiles = _maintenanceService.GetAllBackupFiles().ToList();

            //prepare list model
            var model = new BackupFileListModel
            {
                Data = backupFiles.PaginationByRequestModel(searchModel).Select(file =>
                {
                    //fill in model values from the entity
                    var backupFileModel = new BackupFileModel
                    {
                        Name = _fileProvider.GetFileName(file)
                    };

                    //fill in additional values (not existing in the entity)
                    backupFileModel.Length = $"{_fileProvider.FileLength(file) / 1024f / 1024f:F2} Mb";
                    backupFileModel.Link   = $"{_webHelper.GetStoreLocation(false)}db_backups/{backupFileModel.Name}";

                    return(backupFileModel);
                }),
                Total = backupFiles.Count
            };

            return(model);
        }
        /// <summary>
        /// Get files in the passed directory
        /// </summary>
        /// <param name="directoryPath">Path to the files directory</param>
        /// <param name="type">Type of the files</param>
        /// <returns>A task that represents the completion of the operation</returns>
        protected virtual async Task GetFilesAsync(string directoryPath, string type)
        {
            directoryPath = GetVirtualPath(directoryPath);
            var files = GetFiles(GetFullPath(directoryPath), type);

            await HttpContext.Response.WriteAsync("[");

            for (var i = 0; i < files.Count; i++)
            {
                var width        = 0;
                var height       = 0;
                var physicalPath = files[i];

                if (GetFileType(_fileProvider.GetFileExtension(files[i])) == "image")
                {
                    using (var stream = new FileStream(physicalPath, FileMode.Open))
                    {
                        using (var image = Image.FromStream(stream))
                        {
                            width  = image.Width;
                            height = image.Height;
                        }
                    }
                }

                await HttpContext.Response.WriteAsync($"{{\"p\":\"{directoryPath.TrimEnd('/')}/{_fileProvider.GetFileName(physicalPath)}\",\"t\":\"{Math.Ceiling(GetTimestamp(_fileProvider.GetLastWriteTime(physicalPath)))}\",\"s\":\"{_fileProvider.FileLength(physicalPath)}\",\"w\":\"{width}\",\"h\":\"{height}\"}}");

                if (i < files.Count - 1)
                {
                    await HttpContext.Response.WriteAsync(",");
                }
            }

            await HttpContext.Response.WriteAsync("]");
        }