├── .gitattributes ├── .gitignore ├── HowToFileDisplay ├── Controllers │ └── HomeController.cs ├── HowToFileDisplay.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ └── FileManager.cs ├── Startup.cs ├── Views │ ├── Home │ │ ├── Advanced.cshtml │ │ └── Index.cshtml │ ├── Shared │ │ └── _Layout.cshtml │ ├── _ViewImports.cshtml │ └── _ViewStart.cshtml ├── appsettings.Development.json └── appsettings.json ├── HowToFileUploads.sln ├── HowToFileUploads ├── Controllers │ └── HomeController.cs ├── HowToFileUploads.csproj ├── Models │ └── SomeForm.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── Startup.cs ├── Views │ ├── Home │ │ └── Index.cshtml │ ├── Shared │ │ └── _Layout.cshtml │ ├── _ViewImports.cshtml │ └── _ViewStart.cshtml ├── appsettings.Development.json └── appsettings.json └── HowToVideoFiles ├── BackgroundTasks ├── BackgroundQueue.cs └── QueueService.cs ├── Controllers └── HomeController.cs ├── HowToVideoFiles.csproj ├── Program.cs ├── Properties └── launchSettings.json ├── Startup.cs ├── Views ├── Home │ └── Index.cshtml ├── Shared │ └── _Layout.cshtml ├── _ViewImports.cshtml └── _ViewStart.cshtml ├── appsettings.Development.json └── appsettings.json /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc 262 | 263 | wwwroot/ -------------------------------------------------------------------------------- /HowToFileDisplay/Controllers/HomeController.cs: -------------------------------------------------------------------------------- 1 | using HowToFileDisplay.Services; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Mvc; 4 | 5 | namespace HowToFileDisplay.Controllers 6 | { 7 | public class HomeController : Controller 8 | { 9 | private FileManager _fileManager; 10 | 11 | public HomeController(FileManager fileManager) 12 | { 13 | _fileManager = fileManager; 14 | } 15 | 16 | public IActionResult Index() => View(_fileManager.GetFiles()); 17 | 18 | [HttpPost] 19 | public IActionResult Index(IFormFile file) 20 | { 21 | _fileManager.SaveFile(file); 22 | return RedirectToAction("Index"); 23 | } 24 | 25 | #region advanced 26 | public IActionResult Advanced() => View(_fileManager.GetOptimizedFiles()); 27 | 28 | [HttpPost] 29 | public IActionResult Advanced(IFormFile file) 30 | { 31 | _fileManager.SaveFileOptimize(file); 32 | return RedirectToAction("Advanced"); 33 | } 34 | 35 | public IActionResult GetImage(int id, int width) => 36 | new FileStreamResult(_fileManager.GetImageStream(id, width), "image/*"); 37 | 38 | public IActionResult DownloadImage(int id) => 39 | File(_fileManager.GetImageStream(id, 0), "image/*", "image.png"); 40 | #endregion 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /HowToFileDisplay/HowToFileDisplay.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.2 5 | InProcess 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /HowToFileDisplay/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace HowToFileDisplay 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | CreateWebHostBuilder(args).Build().Run(); 18 | } 19 | 20 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /HowToFileDisplay/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:57741", 7 | "sslPort": 44319 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "HowToFileDisplay": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 22 | "environmentVariables": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /HowToFileDisplay/Services/FileManager.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Hosting; 2 | using Microsoft.AspNetCore.Http; 3 | using PhotoSauce.MagicScaler; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.IO; 7 | using System.Linq; 8 | 9 | namespace HowToFileDisplay.Services 10 | { 11 | public class FileManager 12 | { 13 | private IHostingEnvironment _env; 14 | private List Files; 15 | private int Id; 16 | 17 | private readonly List<(int Width, int Height)> imgSizes = new List<(int, int)> 18 | { 19 | (480, 270), 20 | (640, 360), 21 | (1280, 720), 22 | (1920, 1080) 23 | }; 24 | 25 | public FileManager(IHostingEnvironment env) 26 | { 27 | _env = env; 28 | Files = new List(); 29 | } 30 | 31 | public File GetFile(int id) => Files.FirstOrDefault(x => x.Id == id); 32 | 33 | public File GetFile(int id, int width) => Files.FirstOrDefault(x => x.Id == id && x.Width == width); 34 | 35 | public IEnumerable GetFiles() => Files; 36 | 37 | public IEnumerable GetOptimizedFiles() => 38 | Files 39 | .Where(x => x.Width > 0) 40 | .Select(x => x.Id) 41 | .Distinct(); 42 | 43 | public void SaveFile(IFormFile file) 44 | { 45 | var name = RandomName(); 46 | var save_path = Path.Combine(_env.WebRootPath, name); 47 | 48 | using (var fileStream = new FileStream(save_path, FileMode.Create, FileAccess.Write)) 49 | { 50 | file.CopyTo(fileStream); 51 | } 52 | 53 | Files.Add(new File 54 | { 55 | Id = GetId(), 56 | RelativePath = $"/{name}", 57 | GlobalPath = save_path 58 | }); 59 | } 60 | 61 | private string RandomName(string prefix = "") => 62 | $"img{prefix}_{DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss")}.png"; 63 | 64 | public void SaveFileOptimize(IFormFile file) 65 | { 66 | ProcessImageSettings settings = new ProcessImageSettings 67 | { 68 | ResizeMode = CropScaleMode.Crop, 69 | SaveFormat = FileFormat.Jpeg, 70 | JpegQuality = 100, 71 | JpegSubsampleMode = ChromaSubsampleMode.Subsample420 72 | }; 73 | 74 | int id = GetId(); 75 | 76 | foreach (var (Width, Height) in imgSizes) 77 | { 78 | settings.Width = Width; 79 | settings.Height = Height; 80 | 81 | var name = RandomName(Width.ToString()); 82 | var save_path = Path.Combine(_env.WebRootPath, name); 83 | 84 | using (var fileStream = new FileStream(save_path, FileMode.Create)) 85 | { 86 | MagicImageProcessor.ProcessImage(file.OpenReadStream(), fileStream, settings); 87 | } 88 | 89 | Files.Add(new File 90 | { 91 | Id = id, 92 | Width = Width, 93 | RelativePath = $"/{name}", 94 | GlobalPath = save_path 95 | }); 96 | } 97 | } 98 | 99 | private int GetId() => Id++; 100 | 101 | public FileStream GetImageStream(int id, int width) 102 | { 103 | var path = GetFile(id, GetBestWidth(width)).GlobalPath; 104 | 105 | return new FileStream(path, FileMode.Open, FileAccess.Read); 106 | } 107 | 108 | private int GetBestWidth(int width) 109 | { 110 | foreach (var (Width, Height) in imgSizes) 111 | if (Width >= width) 112 | return Width; 113 | 114 | return imgSizes[imgSizes.Count - 1].Width; 115 | } 116 | } 117 | 118 | public class File 119 | { 120 | public int Id { get; set; } 121 | public int Width { get; set; } 122 | public string RelativePath { get; set; } 123 | public string GlobalPath { get; set; } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /HowToFileDisplay/Startup.cs: -------------------------------------------------------------------------------- 1 | using HowToFileDisplay.Services; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace HowToFileDisplay 7 | { 8 | public class Startup 9 | { 10 | public void ConfigureServices(IServiceCollection services) 11 | { 12 | services.AddMvc(); 13 | 14 | services.AddSingleton(); 15 | } 16 | 17 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 18 | { 19 | app.UseDeveloperExceptionPage(); 20 | 21 | app.UseStaticFiles(); 22 | 23 | app.UseMvcWithDefaultRoute(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /HowToFileDisplay/Views/Home/Advanced.cshtml: -------------------------------------------------------------------------------- 1 | @model IEnumerable 2 | 3 |

Advanced File Display

4 | 5 |
6 |
8 | 9 | 10 |
11 |
12 | 13 | @foreach (var id in Model) 14 | { 15 | 16 | Download 17 | } 18 | 19 | @section scripts { 20 | 37 | } -------------------------------------------------------------------------------- /HowToFileDisplay/Views/Home/Index.cshtml: -------------------------------------------------------------------------------- 1 | @model IEnumerable 2 | 3 |

Simple File Display

4 | 5 |
6 |
8 | 9 | 10 |
11 |
12 | 13 | @foreach (var file in Model) 14 | { 15 | 16 | } -------------------------------------------------------------------------------- /HowToFileDisplay/Views/Shared/_Layout.cshtml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | @ViewBag.Title 7 | 8 | 9 | 13 |
14 | @RenderBody() 15 |
16 | 17 | @RenderSection("scripts", false) 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /HowToFileDisplay/Views/_ViewImports.cshtml: -------------------------------------------------------------------------------- 1 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -------------------------------------------------------------------------------- /HowToFileDisplay/Views/_ViewStart.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "_Layout"; 3 | } 4 | -------------------------------------------------------------------------------- /HowToFileDisplay/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /HowToFileDisplay/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*" 8 | } 9 | -------------------------------------------------------------------------------- /HowToFileUploads.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.572 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HowToFileUploads", "HowToFileUploads\HowToFileUploads.csproj", "{B34EEBB8-A0B1-46DF-9725-22BB92DCAF64}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HowToFileDisplay", "HowToFileDisplay\HowToFileDisplay.csproj", "{F6F13211-0C38-4831-8BF1-DDDDA37508A4}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HowToVideoFiles", "HowToVideoFiles\HowToVideoFiles.csproj", "{C2EA7904-0497-4809-9411-ECA9EA42E4A9}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {B34EEBB8-A0B1-46DF-9725-22BB92DCAF64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {B34EEBB8-A0B1-46DF-9725-22BB92DCAF64}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {B34EEBB8-A0B1-46DF-9725-22BB92DCAF64}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {B34EEBB8-A0B1-46DF-9725-22BB92DCAF64}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {F6F13211-0C38-4831-8BF1-DDDDA37508A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {F6F13211-0C38-4831-8BF1-DDDDA37508A4}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {F6F13211-0C38-4831-8BF1-DDDDA37508A4}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {F6F13211-0C38-4831-8BF1-DDDDA37508A4}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {C2EA7904-0497-4809-9411-ECA9EA42E4A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {C2EA7904-0497-4809-9411-ECA9EA42E4A9}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {C2EA7904-0497-4809-9411-ECA9EA42E4A9}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {C2EA7904-0497-4809-9411-ECA9EA42E4A9}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {8241DF61-A4A3-4E53-B6D7-DFF4124BC5EA} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /HowToFileUploads/Controllers/HomeController.cs: -------------------------------------------------------------------------------- 1 | using HowToFileUploads.Models; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.AspNetCore.Mvc; 5 | using System.Collections.Generic; 6 | using System.IO; 7 | 8 | namespace HowToFileUploads.Controllers 9 | { 10 | public class HomeController : Controller 11 | { 12 | private IHostingEnvironment _env; 13 | private string _dir; 14 | 15 | public HomeController(IHostingEnvironment env) 16 | { 17 | _env = env; 18 | _dir = _env.ContentRootPath; 19 | } 20 | 21 | public IActionResult Index() => View(); 22 | 23 | public IActionResult SingleFile(IFormFile file) 24 | { 25 | using (var fileStream = new FileStream(Path.Combine(_dir, "file.png"), FileMode.Create, FileAccess.Write)) 26 | { 27 | file.CopyTo(fileStream); 28 | } 29 | 30 | return RedirectToAction("Index"); 31 | } 32 | 33 | public IActionResult MultipleFiles(IEnumerable files) 34 | { 35 | int i = 0; 36 | foreach (var file in files) 37 | { 38 | using (var fileStream = new FileStream(Path.Combine(_dir, $"file{i++}.png"), FileMode.Create, FileAccess.Write)) 39 | { 40 | file.CopyTo(fileStream); 41 | } 42 | } 43 | 44 | return RedirectToAction("Index"); 45 | } 46 | 47 | public IActionResult FileInModel(SomeForm someForm) 48 | { 49 | using (var fileStream = new FileStream( 50 | Path.Combine(_dir, $"{someForm.Name}.png"), 51 | FileMode.Create, 52 | FileAccess.Write)) 53 | { 54 | someForm.File.CopyTo(fileStream); 55 | } 56 | 57 | return RedirectToAction("Index"); 58 | } 59 | 60 | } 61 | } -------------------------------------------------------------------------------- /HowToFileUploads/HowToFileUploads.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.2 5 | InProcess 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /HowToFileUploads/Models/SomeForm.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Http; 2 | 3 | namespace HowToFileUploads.Models 4 | { 5 | public class SomeForm 6 | { 7 | public string Name { get; set; } 8 | public IFormFile File { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /HowToFileUploads/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace HowToFileUploads 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | CreateWebHostBuilder(args).Build().Run(); 18 | } 19 | 20 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /HowToFileUploads/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:50792", 7 | "sslPort": 44343 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "HowToFileUploads": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 22 | "environmentVariables": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /HowToFileUploads/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace HowToFileUploads 6 | { 7 | public class Startup 8 | { 9 | public void ConfigureServices(IServiceCollection services) 10 | { 11 | services.AddMvc(); 12 | } 13 | 14 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 15 | { 16 | if (env.IsDevelopment()) 17 | { 18 | app.UseDeveloperExceptionPage(); 19 | } 20 | 21 | app.UseMvcWithDefaultRoute(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /HowToFileUploads/Views/Home/Index.cshtml: -------------------------------------------------------------------------------- 1 | 

Home Page

2 | 3 | 4 | 5 |
6 |

Single File Upload

7 |
9 | 10 | 11 |
12 |
13 | 14 | 15 |
16 |

Mutliple File Upload

17 |
19 | 20 | 21 |
22 |
23 | 24 |
25 |

File in Model Upload

26 |
28 | 29 | 30 | 31 |
32 |
33 | 34 | 35 | 36 |
37 |

Single File Upload (JS)

38 | 39 |
40 | 41 | 42 |
43 |

Multiple File Upload (JS)

44 | 45 |
46 | 47 | 48 |
49 |

File in Model Upload (JS)

50 | 51 | 52 |
53 | 54 | @section scripts { 55 | 89 | } -------------------------------------------------------------------------------- /HowToFileUploads/Views/Shared/_Layout.cshtml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | @ViewBag.Title 7 | 8 | 9 |
10 | @RenderBody() 11 |
12 | 13 | 14 | 15 | @RenderSection("scripts") 16 | 17 | 18 | -------------------------------------------------------------------------------- /HowToFileUploads/Views/_ViewImports.cshtml: -------------------------------------------------------------------------------- 1 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -------------------------------------------------------------------------------- /HowToFileUploads/Views/_ViewStart.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "_Layout"; 3 | } 4 | -------------------------------------------------------------------------------- /HowToFileUploads/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /HowToFileUploads/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*" 8 | } 9 | -------------------------------------------------------------------------------- /HowToVideoFiles/BackgroundTasks/BackgroundQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | namespace HowToVideoFiles.BackgroundTasks 7 | { 8 | public interface IBackgroundQueue 9 | { 10 | void QueueTask(Func task); 11 | 12 | Task> PopQueue(CancellationToken cancellationToken); 13 | } 14 | 15 | public class BackgroundQueue : IBackgroundQueue 16 | { 17 | private ConcurrentQueue> Tasks; 18 | 19 | private SemaphoreSlim Signal; 20 | 21 | public BackgroundQueue() 22 | { 23 | Tasks = new ConcurrentQueue>(); 24 | Signal = new SemaphoreSlim(0); 25 | } 26 | 27 | public void QueueTask(Func task) 28 | { 29 | Tasks.Enqueue(task); 30 | Signal.Release(); 31 | } 32 | 33 | public async Task> PopQueue(CancellationToken cancellationToken) 34 | { 35 | await Signal.WaitAsync(cancellationToken); 36 | Tasks.TryDequeue(out var task); 37 | 38 | return task; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /HowToVideoFiles/BackgroundTasks/QueueService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Hosting; 2 | using System; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | namespace HowToVideoFiles.BackgroundTasks 7 | { 8 | public class QueueService : BackgroundService 9 | { 10 | private IBackgroundQueue _queue; 11 | 12 | public QueueService(IBackgroundQueue queue) 13 | { 14 | _queue = queue; 15 | } 16 | 17 | protected async override Task ExecuteAsync(CancellationToken stoppingToken) 18 | { 19 | while (!stoppingToken.IsCancellationRequested) 20 | { 21 | var task = await _queue.PopQueue(stoppingToken); 22 | 23 | if (stoppingToken.IsCancellationRequested) 24 | { 25 | return; 26 | } 27 | 28 | using (var source = new CancellationTokenSource()) 29 | { 30 | source.CancelAfter(TimeSpan.FromMinutes(1)); 31 | var timeoutToken = source.Token; 32 | 33 | await task(timeoutToken); 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /HowToVideoFiles/Controllers/HomeController.cs: -------------------------------------------------------------------------------- 1 | using HowToVideoFiles.BackgroundTasks; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.AspNetCore.Mvc; 5 | using System; 6 | using System.IO; 7 | using System.Linq; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | using Xabe.FFmpeg; 11 | using Xabe.FFmpeg.Enums; 12 | 13 | namespace HowToVideoFiles.Controllers 14 | { 15 | public class HomeController : Controller 16 | { 17 | private string _dir; 18 | private IBackgroundQueue _queue; 19 | 20 | public HomeController( 21 | IHostingEnvironment env, 22 | IBackgroundQueue queue) 23 | { 24 | _dir = env.WebRootPath; 25 | _queue = queue; 26 | } 27 | 28 | [HttpGet] 29 | public IActionResult Index() => View(); 30 | 31 | [HttpPost] 32 | public async Task UploadFile(IFormFile file) 33 | { 34 | using (var fileStream = 35 | new FileStream(Path.Combine(_dir, "file.mp4"), FileMode.Create, FileAccess.Write)) 36 | { 37 | await file.CopyToAsync(fileStream); 38 | } 39 | 40 | return Ok(); 41 | } 42 | 43 | [HttpPost] 44 | public IActionResult TrimVideo(double start, double end) 45 | { 46 | _queue.QueueTask(async token => 47 | { 48 | await ConvertVideo(start, end, token); 49 | }); 50 | 51 | return RedirectToAction("Index"); 52 | } 53 | 54 | public async Task ConvertVideo(double start, double end, CancellationToken ct) 55 | { 56 | try 57 | { 58 | var input = Path.Combine(_dir, "file.mp4"); 59 | var output = Path.Combine(_dir, "converted.mp4"); 60 | 61 | FFmpeg.ExecutablesPath = Path.Combine(_dir, "ffmpeg"); 62 | 63 | var startSpan = TimeSpan.FromSeconds(start); 64 | var endSpan = TimeSpan.FromSeconds(end); 65 | var duration = endSpan - startSpan; 66 | 67 | var info = await MediaInfo.Get(input); 68 | 69 | var videoStream = info.VideoStreams.First() 70 | .SetCodec(VideoCodec.H264) 71 | .SetSize(VideoSize.Hd480) 72 | .Split(startSpan, duration); 73 | 74 | await Conversion.New() 75 | .AddStream(videoStream) 76 | .SetOutput(output) 77 | .Start(ct); 78 | } 79 | catch (Exception e) 80 | { 81 | Console.WriteLine(e.Message); 82 | } 83 | 84 | return true; 85 | } 86 | 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /HowToVideoFiles/HowToVideoFiles.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.2 5 | InProcess 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /HowToVideoFiles/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace HowToVideoFiles 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | CreateWebHostBuilder(args).Build().Run(); 18 | } 19 | 20 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /HowToVideoFiles/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:50905", 7 | "sslPort": 44383 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "HowToVideoFiles": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 22 | "environmentVariables": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /HowToVideoFiles/Startup.cs: -------------------------------------------------------------------------------- 1 | using HowToVideoFiles.BackgroundTasks; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace HowToVideoFiles 7 | { 8 | public class Startup 9 | { 10 | public void ConfigureServices(IServiceCollection services) 11 | { 12 | services.AddHostedService(); 13 | services.AddSingleton(); 14 | services.AddMvc(); 15 | } 16 | 17 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 18 | { 19 | app.UseDeveloperExceptionPage(); 20 | app.UseStaticFiles(); 21 | app.UseMvcWithDefaultRoute(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /HowToVideoFiles/Views/Home/Index.cshtml: -------------------------------------------------------------------------------- 1 | 

Working with Videos

2 | 3 |
4 |
5 | 6 | 7 |
8 |
9 |
10 |
11 | 12 | 13 | 14 |
15 |
16 |
17 | 18 | 19 |
20 |
21 | 22 |
23 | 25 |
26 | 27 | @section scripts { 28 | 29 | 61 | } -------------------------------------------------------------------------------- /HowToVideoFiles/Views/Shared/_Layout.cshtml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | @ViewBag.Title 7 | 8 | 9 |
10 | @RenderBody() 11 |
12 | 13 | @RenderSection("scripts", false) 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /HowToVideoFiles/Views/_ViewImports.cshtml: -------------------------------------------------------------------------------- 1 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -------------------------------------------------------------------------------- /HowToVideoFiles/Views/_ViewStart.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "_Layout"; 3 | } 4 | -------------------------------------------------------------------------------- /HowToVideoFiles/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /HowToVideoFiles/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*" 8 | } 9 | --------------------------------------------------------------------------------