Commit 0dc26508 authored by SergeevaAA's avatar SergeevaAA

Реализованы просмотр и скачивание файлов

parent 6fa073c9
// <auto-generated />
using System;
using FileDesk.DataAccess.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace FileDesk.DataAccess.Migrations
{
[DbContext(typeof(FileDeskContext))]
[Migration("20201029122140_AddContentTypeFieldInFilesTable")]
partial class AddContentTypeFieldInFilesTable
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("FileDesk.Domain.Models.Action", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<int>("ActionType")
.HasColumnType("int");
b.Property<DateTime>("DateTime")
.HasColumnType("datetime2");
b.Property<long>("FileId")
.HasColumnType("bigint");
b.Property<long>("UserId")
.HasColumnType("bigint");
b.HasKey("Id");
b.HasIndex("FileId");
b.HasIndex("UserId");
b.ToTable("Actions");
});
modelBuilder.Entity("FileDesk.Domain.Models.Contact", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Department")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("MiddleName")
.HasColumnType("nvarchar(max)");
b.Property<string>("Organization")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Phone")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Post")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Contacts");
b.HasData(
new
{
Id = 1L,
Department = "ИШИТР",
Email = "aas189@tpu.ru",
FirstName = "Анастасия",
LastName = "Сергеева",
MiddleName = "Анатольевна",
Organization = "ТПУ",
Phone = "+7(987)654-32-10",
Post = "Студент группы 8ИМ91"
});
});
modelBuilder.Entity("FileDesk.Domain.Models.File", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<byte[]>("Content")
.IsRequired()
.HasColumnType("varbinary(max)");
b.Property<string>("ContentType")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Format")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<long>("Size")
.HasColumnType("bigint");
b.Property<DateTime>("UploadDateTime")
.HasColumnType("datetime2");
b.Property<long>("UserId")
.HasColumnType("bigint");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Files");
});
modelBuilder.Entity("FileDesk.Domain.Models.Role", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Roles");
b.HasData(
new
{
Id = 1L,
Name = "admin"
},
new
{
Id = 2L,
Name = "user"
});
});
modelBuilder.Entity("FileDesk.Domain.Models.User", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Login")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.HasIndex("Login")
.IsUnique();
b.ToTable("Users");
b.HasData(
new
{
Id = 1L,
Login = "admin",
Password = "5EBE2294ECD0E0F08EAB7690D2A6EE69"
},
new
{
Id = 2L,
Login = "user",
Password = "93AB957B29BBD2564F1850110F29120D"
});
});
modelBuilder.Entity("FileDesk.Domain.Models.UserRole", b =>
{
b.Property<long>("UserId")
.HasColumnType("bigint");
b.Property<long>("RoleId")
.HasColumnType("bigint");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("UserRoles");
b.HasData(
new
{
UserId = 1L,
RoleId = 1L
},
new
{
UserId = 1L,
RoleId = 2L
},
new
{
UserId = 2L,
RoleId = 2L
});
});
modelBuilder.Entity("FileDesk.Domain.Models.Action", b =>
{
b.HasOne("FileDesk.Domain.Models.File", "File")
.WithMany("Actions")
.HasForeignKey("FileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FileDesk.Domain.Models.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FileDesk.Domain.Models.File", b =>
{
b.HasOne("FileDesk.Domain.Models.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FileDesk.Domain.Models.UserRole", b =>
{
b.HasOne("FileDesk.Domain.Models.Role", "Role")
.WithMany("UserRoles")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FileDesk.Domain.Models.User", "User")
.WithMany("UserRoles")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace FileDesk.DataAccess.Migrations
{
public partial class AddContentTypeFieldInFilesTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ContentType",
table: "Files",
nullable: false,
defaultValue: "");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ContentType",
table: "Files");
}
}
}
......@@ -115,6 +115,10 @@ namespace FileDesk.DataAccess.Migrations
.IsRequired()
.HasColumnType("varbinary(max)");
b.Property<string>("ContentType")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
......
namespace FileDesk.Domain.Enums
{
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.GetName();
}
}
}
\ No newline at end of file
......@@ -13,40 +13,34 @@
[Display(Name = "Неизвестный формат")]
Unknown = 0,
/// <summary>
/// Текстовый файл
/// </summary>
[Display(Name = "Текстовый файл")]
Text = 1,
/// <summary>
/// Документ
/// </summary>
[Display(Name = "Документ")]
Document = 2,
Document = 1,
/// <summary>
/// Изображение
/// </summary>
[Display(Name = "Изображение")]
Picture = 3,
Picture = 2,
/// <summary>
/// Аудиофайл
/// </summary>
[Display(Name = "Аудиофайл")]
Audio = 4,
Audio = 3,
/// <summary>
/// Видеофайл
/// </summary>
[Display(Name = "Видеофайл")]
Video = 5,
Video = 4,
/// <summary>
/// Исполняемый файл
/// </summary>
[Display(Name = "Исполняемый файл")]
Executed = 6
Executed = 5
}
}
\ No newline at end of file
......@@ -35,6 +35,12 @@
[Required]
public virtual byte[] Content { get; set; }
/// <summary>
/// MIME-тип содержимого
/// </summary>
[Required]
public virtual string ContentType { get; set; }
/// <summary>
/// Формат
/// </summary>
......
namespace FileDesk.Domain.ViewModels
{
public class FileResultViewModel
{
/// <summary>
/// Название
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// MIME-тип содержимого
/// </summary>
public virtual string ContentType { get; set; }
/// <summary>
/// Содержимое файла
/// </summary>
public virtual byte[] Content { get; set; }
}
}
\ No newline at end of file
namespace FileDesk.Domain.ViewModels
{
using System;
using FileDesk.Domain.Enums;
using FileDesk.Domain.Models;
/// <summary>
/// Вью-модель файла
/// </summary>
public class FileViewModel : AbstractEntity
{
/// <summary>
/// Название
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// Описание
/// </summary>
public virtual string Description { get; set; }
/// <summary>
/// Дата и время добавления
/// </summary>
public virtual DateTime UploadDateTime { get; set; }
/// <summary>
/// Формат
/// </summary>
public virtual string Format { get; set; }
/// <summary>
/// Отображаемое название формата
/// </summary>
public virtual string FormatDisplayName { get; set; }
/// <summary>
/// Пользователь
/// </summary>
public virtual string User { get; set; }
/// <summary>
/// Размер в байтах
/// </summary>
public virtual string Size { get; set; }
}
}
\ No newline at end of file
namespace FileDesk.Domain.ViewModels
{
using Microsoft.AspNetCore.Http;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
/// <summary>
/// Вью-модель файла для загрузки
/// </summary>
......@@ -12,21 +13,21 @@
/// <summary>
/// Название
/// </summary>
[Required]
[Required(ErrorMessage = "Не указано название файла")]
[DisplayName("Название")]
public virtual string Name { get; set; }
/// <summary>
/// Описание
/// </summary>
[Required]
[Required(ErrorMessage = "Не указано описание файла")]
[DisplayName("Описание")]
public virtual string Description { get; set; }
/// <summary>
/// Файл
/// </summary>
[Required]
[Required(ErrorMessage = "Не выбран файл")]
[DisplayName("Файл")]
public virtual IFormFile File { get; set; }
}
......
......@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Http;
......@@ -10,10 +11,12 @@
using FileDesk.Domain.Enums;
using FileDesk.Domain.ViewModels;
using FileDesk.Services.Interfaces;
using Action = Domain.Models.Action;
using File = Domain.Models.File;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.ComponentModel.DataAnnotations;
/// <summary>
/// Сервис для работы с файлами
......@@ -22,6 +25,11 @@
{
private readonly FileDeskContext _db;
private const double kb = 1024;
private const double mb = 1048576;
private const double gb = 1073741824;
private const double tb = 1099511627776;
/// <summary>
/// Создает новый экземпляр класса <see cref = "FileService"/>
/// </summary>
......@@ -31,6 +39,53 @@
_db = db;
}
/// <summary>
/// Получение списка файлов
/// </summary>
/// <returns>Список файлов</returns>
public List<FileViewModel> GetAll()
{
return _db.Files.Include(f => f.User).OrderByDescending(f => f.UploadDateTime)
.Select(f => new FileViewModel
{
Id = f.Id,
Name = f.Name,
Description = f.Description,
UploadDateTime = f.UploadDateTime,
Size = GetSize(f.Size),
Format = f.Format.ToString(),
FormatDisplayName = f.Format.GetDisplayName(),
User = f.User.Login
}).ToList();
}
/// <summary>
/// Скачивание файла
/// </summary>
/// <param name="id">Идентификатор файла</param>
/// <param name="id">Идентификатор пользователя</param>
/// <returns>Файл</returns>