public async Task <Site> CreateSite(string newSiteName, bool openToUpload, bool openToDownload, string appid) { await _createSiteLock.WaitAsync(); try { await _dbContext.Sites.EnsureUniqueString(t => t.SiteName, newSiteName); var newRootFolder = new Folder { FolderName = "blob" }; await _dbContext.Folders.AddAsync(newRootFolder); await _dbContext.SaveChangesAsync(); var site = new Site { AppId = appid, SiteName = newSiteName.ToLower(), RootFolderId = newRootFolder.Id, OpenToUpload = openToUpload, OpenToDownload = openToDownload }; await _dbContext.Sites.AddAsync(site); await _dbContext.SaveChangesAsync(); return(site); } finally { _createSiteLock.Release(); } }
public async Task <IActionResult> CreateNewFolder(CreateNewFolderAddressModel model) { var folders = _folderLocator.SplitStrings(model.FolderNames); var folder = await _folderLocator.LocateSiteAndFolder(model.AccessToken, model.SiteName, folders, model.RecursiveCreate); var conflict = await _dbContext .Folders .Where(t => t.ContextId == folder.Id) .AnyAsync(t => t.FolderName == model.NewFolderName.ToLower()); if (conflict) { return(this.Protocol(ErrorType.HasDoneAlready, $"Folder name: '{model.NewFolderName}' conflict!")); } var newFolder = new Folder { ContextId = folder.Id, FolderName = model.NewFolderName.ToLower(), }; _dbContext.Folders.Add(newFolder); await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.Success, "Successfully created your new folder!")); }
public async Task DeleteFileById(int fileId) { var file = await _dbContext.Files.SingleOrDefaultAsync(t => t.Id == fileId); if (file != null) { await DeleteFile(file); } await _dbContext.SaveChangesAsync(); }
public Task CreateNewFolder(int contextId, string name) { var newFolder = new Folder { ContextId = contextId, FolderName = name.ToLower(), }; _dbContext.Folders.Add(newFolder); return(_dbContext.SaveChangesAsync()); }
public async Task <Folder> LocateAsync(string[] folderNames, Folder root, bool recursiveCreate) { var currentFolder = root; foreach (var folder in folderNames) { var folderObject = await _dbContext .Folders .Include(t => t.SubFolders) .Include(t => t.Files) .Include(t => t.Context) .Where(t => t.ContextId == currentFolder.Id) .SingleOrDefaultAsync(t => t.FolderName == folder.ToLower()); if (recursiveCreate && folderObject == null && !string.IsNullOrWhiteSpace(folder)) { folderObject = new Folder { ContextId = currentFolder.Id, FolderName = folder, Files = new List <File>(), SubFolders = new List <Folder>() }; _dbContext.Folders.Add(folderObject); await _dbContext.SaveChangesAsync(); } currentFolder = folderObject ?? throw new AiurAPIModelException(ErrorType.NotFound, $"Not found folder '{folder}' under folder '{currentFolder.FolderName}'!"); } return(currentFolder); }
private async Task AllClean(ProbeDbContext dbContext, IStorageProvider storageProvider) { var files = await dbContext.Files.ToListAsync(); foreach (var file in files) { if (!storageProvider.ExistInHardware(file.HardwareId)) { _logger.LogInformation($"Cleaner message: File with Id: {file.HardwareId} was found in database but not found on disk! Deleting record in database..."); // delete file in db. dbContext.Files.Remove(file); } await dbContext.SaveChangesAsync(); } var storageFiles = storageProvider.GetAllFileNamesInHardware(); foreach (var file in storageFiles) { if (!await dbContext.Files.AnyAsync(t => t.HardwareId == file)) { _logger.LogCritical($"Cleaner message: File with harewareId: {file} was found on disk but not found in database! Consider Delet file on disk!"); } } return; }
private async Task AllClean(ProbeDbContext dbContext, IStorageProvider storageProvider) { var files = await dbContext.Files.ToListAsync(); foreach (var file in files) { if (!storageProvider.ExistInHardware(file.HardwareId)) { _logger.LogWarning($"Cleaner message: File with Id: {file.HardwareId} was found in database but not found on disk! Deleting record in database..."); // delete file in db. dbContext.Files.Remove(file); } } await dbContext.SaveChangesAsync(); var storageFiles = storageProvider.GetAllFileNamesInHardware(); foreach (var file in storageFiles) { if (files.All(t => t.HardwareId != file)) { _logger.LogWarning($"Cleaner message: File with hardware Id: {file} was found on disk but not found in database! Consider Delete that file on disk!"); // delete file in disk storageProvider.DeleteToTrash(file); } } }
public async Task <IActionResult> DeleteFile(DeleteFileAddressModel model) { var(folders, fileName) = _folderLocator.SplitToPath(model.FolderNames); var folder = await _folderLocator.LocateSiteAndFolder(model.AccessToken, model.SiteName, folders); var file = folder.Files.SingleOrDefault(t => t.FileName == fileName); if (file == null) { return(this.Protocol(ErrorType.NotFound, "The file cannot be found. Maybe it has been deleted.")); } _folderCleaner.DeleteFile(file); await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.Success, $"Successfully deleted the file '{file.FileName}'")); }
private async Task AllClean(ProbeDbContext dbContext) { var files = await dbContext.Files.ToListAsync(); foreach (var file in files) { var path = _configuration["StoragePath"] + $"{_}Storage{_}{file.Id}.dat"; if (!File.Exists(path)) { _logger.LogInformation($"Cleaner message: File with Id: {file.Id} was found in database but not found on disk! Deleting record in database..."); // delete file in db. dbContext.Files.Remove(file); } else { file.FileSize = new FileInfo(path).Length; } await dbContext.SaveChangesAsync(); } var storageFiles = Directory.GetFiles(_configuration["StoragePath"] + $"{_}Storage"); foreach (var file in storageFiles) { var fileName = Convert.ToInt32(Path.GetFileNameWithoutExtension(file)); if (!await dbContext.Files.AnyAsync(t => t.Id == fileName)) { _logger.LogInformation($"Cleaner message: File with Id: {fileName} was found on disk but not found in database! Deleting file on disk..."); // delete file on disk. //File.Delete(file); } } return; }
public async Task <ProbeApp> GetApp(string accessToken) { var appid = _acTokenManager.ValidateAccessToken(accessToken); var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.AppId == appid); if (appLocal == null) { appLocal = new ProbeApp { AppId = appid }; await _dbContext.Apps.AddAsync(appLocal); await _dbContext.SaveChangesAsync(); } return(appLocal); }
public async Task CreateNewFolder(int contextId, string name) { await _folderCreateLock.WaitAsync(); try { await _dbContext.Folders .Where(t => t.ContextId == contextId) .EnsureUniqueString(t => t.FolderName, name); var newFolder = new Folder { ContextId = contextId, FolderName = name.ToLower(), }; _dbContext.Folders.Add(newFolder); await _dbContext.SaveChangesAsync(); } finally { _folderCreateLock.Release(); } }
public async Task <Site> CreateSite(string newSiteName, bool openToUpload, bool openToDownload, string appid) { var newRootFolder = new Folder { FolderName = "blob" }; await _dbContext.Folders.AddAsync(newRootFolder); await _dbContext.SaveChangesAsync(); var site = new Site { AppId = appid, SiteName = newSiteName.ToLower(), RootFolderId = newRootFolder.Id, OpenToUpload = openToUpload, OpenToDownload = openToDownload }; await _dbContext.Sites.AddAsync(site); await _dbContext.SaveChangesAsync(); return(site); }
public async Task <IActionResult> CreateNewSite(CreateNewSiteAddressModel model) { var appid = await _tokenManager.ValidateAccessToken(model.AccessToken); var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.AppId == appid); if (appLocal == null) { appLocal = new ProbeApp { AppId = appid }; _dbContext.Apps.Add(appLocal); await _dbContext.SaveChangesAsync(); } var conflict = await _dbContext.Sites .AnyAsync(t => t.SiteName.ToLower().Trim() == model.NewSiteName.ToLower().Trim()); if (conflict) { return(this.Protocol(ErrorType.NotEnoughResources, $"There is already a site with name: '{model.NewSiteName}'. Please try another new name.")); } var newRootFolder = new Folder { FolderName = "blob" }; _dbContext.Folders.Add(newRootFolder); await _dbContext.SaveChangesAsync(); var site = new Site { AppId = appid, SiteName = model.NewSiteName.ToLower(), FolderId = newRootFolder.Id, OpenToUpload = model.OpenToUpload, OpenToDownload = model.OpenToDownload }; _dbContext.Sites.Add(site); await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.Success, $"Successfully created your new site: '{site.SiteName}'.")); }
public async Task <IActionResult> UploadFile(UploadFileAddressModel model) { var site = await _dbContext .Sites .Include(t => t.Root) .SingleOrDefaultAsync(t => t.SiteName.ToLower() == model.SiteName.ToLower()); if (site == null) { return(this.Protocol(ErrorType.NotFound, $"Can't find a site with name: '{model.SiteName}'!")); } if (!site.OpenToUpload) { _tokenEnsurer.Ensure(model.Token, "Upload", model.SiteName, model.FolderNames); } var folders = _folderLocator.SplitToFolders(model.FolderNames); var folder = await _folderLocator.LocateAsync(folders, site.Root, model.RecursiveCreate); if (folder == null) { return(this.Protocol(ErrorType.NotFound, $"Can't locate your folder!")); } try { var _ = HttpContext.Request.Form.Files.FirstOrDefault()?.ContentType; } catch (InvalidOperationException e) { return(this.Protocol(ErrorType.InvalidInput, e.Message)); } // Executing here will let the browser upload the file. if (HttpContext.Request.Form.Files.Count < 1) { return(this.Protocol(ErrorType.InvalidInput, "Please provide a file!")); } var file = HttpContext.Request.Form.Files.First(); if (!new ValidFolderName().IsValid(file.FileName)) { return(this.Protocol(ErrorType.InvalidInput, $"Invalid file name: '{file.FileName}'!")); } var newFile = new File { FileName = _folderLocator.GetValidFileName(folder.Files.Select(t => t.FileName), file.FileName), //file.FileName, ContextId = folder.Id, FileSize = file.Length }; // Save to disk await _storageProvider.Save(newFile.HardwareId, file); // Save to database _dbContext.Files.Add(newFile); await _dbContext.SaveChangesAsync(); var filePath = _probeLocator.GetProbeFullPath(model.SiteName, string.Join('/', folders), newFile.FileName); return(Json(new UploadFileViewModel { InternetPath = _probeLocator.GetProbeOpenAddress(filePath), DownloadPath = _probeLocator.GetProbeDownloadAddress(filePath), SiteName = model.SiteName, FilePath = filePath, FileSize = file.Length, Code = ErrorType.Success, Message = "Successfully uploaded your file." })); }