コード例 #1
0
        private void SeedFiles(FileUploadSystemDbContext context)
        {
            FileManipulator.GenerateStorage();

            if (context.Files.Any())
            {
                return;
            }

            var sampleData = this.GetSampleFiles("Cats");
            var admin      = context.Users.Where(u => u.UserName == "Administrator").FirstOrDefault();

            AddFilesToUser(context, sampleData, admin);

            var dir = new DirectoryEntity
            {
                Name       = "Directory Test",
                LocationId = -1,
                Owner      = admin,
                OwnerId    = admin.Id
            };

            admin.Files.Add(dir);
            context.SaveChanges();

            var innerDirectory = new DirectoryEntity
            {
                Name       = "Inner Directory Test",
                LocationId = dir.Id,
                Owner      = admin,
                OwnerId    = admin.Id
            };

            dir.FilesContained.Add(innerDirectory);
            context.SaveChanges();

            var someFileAgain = new BinaryFile
            {
                Name       = "someFileAgain" + Path.GetExtension(sampleData[0]),
                LocationId = innerDirectory.Id,
                Owner      = admin,
                OwnerId    = admin.Id,
                Size       = new FileInfo(sampleData[0]).Length,
                Type       = Path.GetExtension(sampleData[0])
            };

            innerDirectory.FilesContained.Add(someFileAgain);
            context.SaveChanges();

            FileManipulator.UploadFile(sampleData[0], someFileAgain.Id, Path.GetExtension(sampleData[0]));

            var anotherUSer = context.Users.Where(u => u.UserName == "AnotherUser").FirstOrDefault();
            var dogs        = this.GetSampleFiles("Dogs");

            AddFilesToUser(context, dogs, anotherUSer);
            context.SaveChanges();
        }
コード例 #2
0
        private void SeedUsers(FileUploadSystemDbContext context)
        {
            if (context.Users.Any())
            {
                return;
            }

            var users = new User[]
            {
                new User
                {
                    Email         = "*****@*****.**",
                    UserName      = "******",
                    SecurityStamp = Guid.NewGuid().ToString()
                },
                new User
                {
                    Email         = "*****@*****.**",
                    UserName      = "******",
                    SecurityStamp = Guid.NewGuid().ToString()
                },
                new User
                {
                    Email         = "*****@*****.**",
                    UserName      = "******",
                    SecurityStamp = Guid.NewGuid().ToString()
                }
            };

            for (int i = 0; i < users.Length; i++)
            {
                this.userManager.Create(users[i], "pass123");
            }

            var admin = new User {
                Email = "*****@*****.**", UserName = "******"
            };

            this.userManager.Create(admin, "adminPass123!");

            this.userManager.AddToRole(admin.Id, GlobalConstants.AdminRole);
        }
コード例 #3
0
        private static void AddFilesToUser(FileUploadSystemDbContext context, string[] locations, User currentUser)
        {
            for (int i = 0; i < locations.Length; i++)
            {
                var extension = Path.GetExtension(locations[i]);
                var someFile  = new BinaryFile
                {
                    Name       = Path.GetFileName(locations[i]),
                    LocationId = -1,
                    Owner      = currentUser,
                    OwnerId    = currentUser.Id,
                    Size       = new FileInfo(locations[i]).Length,
                    Type       = extension
                };

                currentUser.Files.Add(someFile);
                context.SaveChanges();

                FileManipulator.UploadFile(locations[i], someFile.Id, extension);
            }
        }
コード例 #4
0
 private void SeedRoles(FileUploadSystemDbContext context)
 {
     context.Roles.AddOrUpdate(x => x.Name, new IdentityRole(GlobalConstants.AdminRole));
     context.SaveChanges();
 }