// this method upload files on the hard drive and saves them in the database public async Task <ICollection <ElementVM> > UploadFiles(IFormFileCollection files, FolderVM parentFolder) { List <ElementVM> elements = new List <ElementVM>(); foreach (var uploadedFile in files) { string path = parentFolder.Path + '\\' + uploadedFile.FileName; if (!System.IO.File.Exists(path)) { DAL.Entities.File file = new DAL.Entities.File { Id = Guid.NewGuid().ToString(), Path = path, IsShared = parentFolder.IsShared, FolderId = parentFolder.Id }; _database.GetRepository <DAL.Entities.File>().Create(file); ElementVM elem = new ElementVM { Id = file.Id, IsFolder = false, ElemName = uploadedFile.FileName, LastModified = DateTime.Now.ToShortDateString(), Type = ElementHelperClass.DefineFileType(ElementHelperClass.DefineFileExtension(uploadedFile.FileName)), Size = ElementHelperClass.Recount(new Tuple <double, ElementVM.Unit>(uploadedFile.Length, ElementVM.Unit.B)), IsShared = file.IsShared }; elements.Add(elem); } using (var fileStream = new FileStream(path, FileMode.Create)) { await uploadedFile.CopyToAsync(fileStream); } } _database.Save(); return(elements); }
// this method returns all folders and files that are contained in the needed folder public ICollection <ElementVM> GetElementsFromFolder(FolderVM folderVM, string userId) { if (folderVM == null) { throw new StatusCodeException("The needed folder doesn't exists.", StatusCodes.Status404NotFound); } DirectoryInfo dir = new DirectoryInfo(folderVM.Path); FileInfo[] files; DirectoryInfo[] dirs; files = dir.GetFiles(); dirs = dir.GetDirectories(); List <ElementVM> models = new List <ElementVM>(); List <Folder> childFolders = _database.GetRepository <Folder>().Find(f => f.ParentFolderId == folderVM.Id).ToList(); List <DAL.Entities.File> childFiles = _database.GetRepository <DAL.Entities.File>().Find(f => f.FolderId == folderVM.Id).ToList(); foreach (var elem in dirs) { Folder folder = childFolders.Find(f => f.Path == elem.FullName); if (folder != null) { if (CheckAccessToView(new FolderVM { OwnerId = folder.OwnerId, IsShared = folder.IsShared }, userId)) { ElementVM model = new ElementVM { Id = folder.Id, IsFolder = true, Type = ElementVM.FileType.Folder, ElemName = elem.Name, LastModified = elem.LastWriteTime.ToShortDateString(), Size = null, IsShared = folder.IsShared }; models.Add(model); } } } foreach (var elem in files) { DAL.Entities.File file = childFiles.Find(f => f.Path == elem.FullName); if (file != null) { FileVM vm = new FileVM { IsShared = file.IsShared, OwnerId = _database.GetRepository <Folder>().Get(file.FolderId).OwnerId }; if (CheckAccessToView(vm, userId)) { // for a more convenient display of file size, call the conversion function Tuple <double, ElementVM.Unit> size = new Tuple <double, ElementVM.Unit>(elem.Length, ElementVM.Unit.B); size = ElementHelperClass.Recount(size); ElementVM.FileType type = ElementHelperClass.DefineFileType(elem.Extension); ElementVM model = new ElementVM { Id = childFiles.Find(f => f.Path == elem.FullName).Id, IsFolder = false, Type = type, ElemName = elem.Name, LastModified = elem.LastWriteTime.ToShortDateString(), Size = size, IsShared = file.IsShared }; models.Add(model); } } } return(models); }