Пример #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
        public ServiceReview UploadFile(HttpPostedFileBase uploadFile, int?currentDirectory, User currenUser, IFileUploadSystemData data)
        {
            var res = new ServiceReview();
            var currentDirectoryId = currentDirectory ?? -1;

            res.RedirectId = currentDirectory;
            var errorFound = false;

            if (uploadFile == null ||
                uploadFile.ContentLength == 0 ||
                uploadFile.ContentLength > GlobalConstants.MaxUploadFileSize ||
                uploadFile.FileName.Trim().Length < 1)
            {
                res.Message    = "Bad file name/size!";
                res.ErrorFound = true;
                return(res);
            }

            var fileName      = Path.GetFileName(uploadFile.FileName);
            var fileExtension = Path.GetExtension(uploadFile.FileName);

            // Check if the same filename allready exists in current directory
            errorFound = CommonFunc.SameFileExistInSameDir(fileName, currentDirectoryId, currenUser);
            if (errorFound)
            {
                res.ErrorFound = true;
                res.Message    = "A file with that name already exists in the current directory!";
                return(res);
            }

            var newFile = new BinaryFile
            {
                Name       = fileName,
                Type       = fileExtension,
                Size       = uploadFile.ContentLength,
                Owner      = currenUser,
                OwnerId    = currenUser.Id,
                LocationId = currentDirectoryId
            };

            if (currentDirectory != -1)
            {
                ((DirectoryEntity)currenUser.Files.Where(f => f.Id == currentDirectory).FirstOrDefault()).FilesContained.Add(newFile);
            }
            else
            {
                currenUser.Files.Add(newFile);
            }

            data.SaveChanges();
            int id = newFile.Id;

            FileManipulator.UploadFile(uploadFile, id, fileExtension);

            res.Message = string.Format("File ({0}) uploaded successfully!", fileName);
            return(res);
        }
Пример #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);
            }
        }