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> Manifest() { var owner = (await _userManager.GetUsersInRoleAsync(Consts.OwnerRoleName)) .First(); var descriptionAttribute = Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyDescriptionAttribute>().Description; var model = new ManifestModel { ShortName = owner.NickName, Name = owner.NickName + "'s Blog", Description = descriptionAttribute, StartUrl = "/", Icons = new List <ManifestIcon>() { new ManifestIcon { Src = _probeLocator.GetProbeOpenAddress(owner.IconFilePath) + "?w=48&square=true", Sizes = "48x48", Type = "image/png" }, new ManifestIcon { Src = _probeLocator.GetProbeOpenAddress(owner.IconFilePath) + ".png?w=72&square=true", Sizes = "72x72", Type = "image/png" }, new ManifestIcon { Src = _probeLocator.GetProbeOpenAddress(owner.IconFilePath) + ".png?w=144&square=true", Sizes = "144x144", Type = "image/png" }, new ManifestIcon { Src = _probeLocator.GetProbeOpenAddress(owner.IconFilePath) + ".png?w=240&square=true", Sizes = "240x240", Type = "image/png" }, new ManifestIcon { Src = _probeLocator.GetProbeOpenAddress(owner.IconFilePath) + ".png?w=512&square=true", Sizes = "512x512", Type = "image/png" } }, BackgroundColor = "#3097D1", ThemeColor = "#3097D1", Display = "standalone", Orientation = "portrait" }; return(Json(model)); }
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 <string> GetUserImageUrl(ClaimsPrincipal userClaims) { var user = await _userManager.GetUserAsync(userClaims); return(_probeLocator.GetProbeOpenAddress(user.IconFilePath)); }
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." })); }