public async Task <IActionResult> CreateNewSite(CreateNewSiteAddressModel model) { var appid = await _appRepo.GetAppId(model.AccessToken); var conflict = await _siteRepo.GetSiteByName(model.NewSiteName) != null; if (conflict) { return(this.Protocol(ErrorType.NotEnoughResources, $"There is already a site with name: '{model.NewSiteName}'. Please try another new name.")); } var createdSite = await _siteRepo.CreateSite(model.NewSiteName, model.OpenToUpload, model.OpenToDownload, appid); return(this.Protocol(ErrorType.Success, $"Successfully created your new site: '{createdSite.SiteName}' at {createdSite.CreationTime}.")); }
public async Task <IActionResult> UploadFile(UploadFileAddressModel model) { var site = await _siteRepo.GetSiteByName(model.SiteName); 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 = _folderSplitter.SplitToFolders(model.FolderNames); var rootFolder = await _folderRepo.GetFolderFromId(site.RootFolderId); var folder = await _folderRepo.GetFolderFromPath(folders, rootFolder, model.RecursiveCreate); if (folder == null) { return(this.Protocol(ErrorType.NotFound, "Can't find your folder!")); } // Executing here will let the browser upload the file. try { var _ = HttpContext.Request.Form.Files.FirstOrDefault()?.ContentType; } catch (InvalidOperationException e) { return(this.Protocol(ErrorType.InvalidInput, e.Message)); } 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 fileName = _folderSplitter.GetValidFileName(folder.Files.Select(t => t.FileName), file.FileName); var newFileHardwareId = await _fileRepo.SaveFileToDb(fileName, folder.Id, file.Length); await _storageProvider.Save(newFileHardwareId, file); var filePath = _probeLocator.GetProbeFullPath(model.SiteName, string.Join('/', folders), fileName); return(Ok(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." })); }
public async Task <IActionResult> Open(OpenAddressModel model) { var site = await _siteRepo.GetSiteByName(model.SiteName); if (site == null) { return(NotFound()); } if (!site.OpenToDownload) { _tokenEnsurer.Ensure(model.PBToken, "Download", model.SiteName, model.FolderNames); } var(folders, fileName) = _folderSplitter.SplitToFoldersAndFile(model.FolderNames); try { var siteRoot = await _folderRepo.GetFolderFromId(site.RootFolderId); var folder = await _folderRepo.GetFolderFromPath(folders, siteRoot, false); if (folder == null) { return(NotFound()); } var file = await _fileRepo.GetFileInFolder(folder, fileName); if (file == null) { return(NotFound()); } var path = _storageProvider.GetFilePath(file.HardwareId); var extension = _storageProvider.GetExtension(file.FileName); if (ControllerContext.ActionDescriptor.AttributeRouteInfo.Name == "File") { return(this.WebFile(path, "do-not-open")); } else if (ControllerContext.ActionDescriptor.AttributeRouteInfo.Name == "Video") { return(VideoPlayerWithFile( probeLocator.GetProbeOpenAddress(model.SiteName, folders, fileName), model.PBToken, fileName)); } else if (file.FileName.IsStaticImage() && Image.DetectFormat(path) != null) { return(await FileWithImageCompressor(path, extension)); } else { return(this.WebFile(path, extension)); } } catch (AiurAPIModelException e) when(e.Code == ErrorType.NotFound) { return(NotFound()); } }
public async Task <IActionResult> UpdateSiteInfo(UpdateSiteInfoAddressModel model) { var appid = await _appRepo.GetAppId(model.AccessToken); var site = await _siteRepo.GetSiteByNameUnderApp(model.OldSiteName, appid); // Conflict = Name changed, and new name already exists. var conflict = model.NewSiteName.ToLower() != model.OldSiteName.ToLower() && await _siteRepo.GetSiteByName(model.NewSiteName) != null; if (conflict) { return(this.Protocol(ErrorType.Conflict, $"There is already a site with name: '{model.NewSiteName}'. Please try another new name.")); } site.SiteName = model.NewSiteName; site.OpenToDownload = model.OpenToDownload; site.OpenToUpload = model.OpenToUpload; await _siteRepo.UpdateSite(site); return(this.Protocol(ErrorType.Success, "Successfully updated your site!")); }