Skip to content

Commit d11d245

Browse files
authored
Merge pull request #105 from telerik/filemanager-blobstorage
chore: add file manager blob storage example
2 parents 5f5bb7b + 38198f3 commit d11d245

File tree

124 files changed

+140407
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+140407
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KendoUI.FileManager.BlobStorage", "KendoUI.FileManager.BlobStorage\KendoUI.FileManager.BlobStorage.csproj", "{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Debug|x64.ActiveCfg = Debug|Any CPU
21+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Debug|x64.Build.0 = Debug|Any CPU
22+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Debug|x86.ActiveCfg = Debug|Any CPU
23+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Debug|x86.Build.0 = Debug|Any CPU
24+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Release|x64.ActiveCfg = Release|Any CPU
27+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Release|x64.Build.0 = Release|Any CPU
28+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Release|x86.ActiveCfg = Release|Any CPU
29+
{8C002550-8D5E-43CE-8F1F-A3A7D5149F48}.Release|x86.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
using KendoUI.FileManager.BlobStorage.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using KendoUI.FileManager.BlobStorage.Services;
4+
using System.Diagnostics;
5+
using System.Text.Json;
6+
7+
namespace KendoUI.FileManager.BlobStorage.Controllers
8+
{
9+
public class HomeController : Controller
10+
{
11+
private readonly ILogger<HomeController> _logger;
12+
// Inject the service that handles Azure Blob Storage operations
13+
private readonly IBlobFileManagerService _fileManagerService;
14+
15+
public HomeController(ILogger<HomeController> logger, IBlobFileManagerService fileManagerService)
16+
{
17+
_logger = logger;
18+
_fileManagerService = fileManagerService;
19+
}
20+
21+
public IActionResult Index()
22+
{
23+
return View();
24+
}
25+
26+
public IActionResult Alternative()
27+
{
28+
return View("Index_Alternative");
29+
}
30+
31+
public IActionResult About()
32+
{
33+
ViewData["Message"] = "Your application description page.";
34+
return View();
35+
}
36+
37+
public IActionResult Contact()
38+
{
39+
ViewData["Message"] = "Your contact page.";
40+
return View();
41+
}
42+
43+
public IActionResult Error()
44+
{
45+
return View();
46+
}
47+
48+
// Handles reading files and folders from Azure Blob Storage for the FileManager
49+
[HttpPost]
50+
public async Task<IActionResult> FileManager_Read([FromForm] FileManagerReadRequest request)
51+
{
52+
try
53+
{
54+
// Retrieve the list of blobs/folders from the specified path
55+
var target = NormalizePath(request.Target ?? request.Path ?? string.Empty);
56+
var files = await _fileManagerService.ReadAsync(target);
57+
return Json(files);
58+
}
59+
catch (Exception ex)
60+
{
61+
_logger.LogError(ex, "Failed to read FileManager contents.");
62+
return BadRequest(new { error = ex.Message });
63+
}
64+
}
65+
66+
// Handles creating new folders or copying/pasting files in Azure Blob Storage
67+
[HttpPost]
68+
public async Task<IActionResult> FileManager_Create([FromForm] FileManagerCreateRequest request)
69+
{
70+
try
71+
{
72+
var target = NormalizePath(request.Target ??
73+
request.Path ??
74+
request.Source ??
75+
request.SourcePath ??
76+
string.Empty);
77+
var name = request.Name?.Trim();
78+
var entry = request.Entry;
79+
80+
if (string.IsNullOrWhiteSpace(name))
81+
{
82+
return BadRequest(new { error = "Name is required for create operation" });
83+
}
84+
85+
// Parse the form data to determine if this is a folder creation, file upload, or copy operation
86+
var context = FileManagerCreateContext.FromRequest(request);
87+
var result = await _fileManagerService.CreateAsync(target, name, entry, context);
88+
return Json(result);
89+
}
90+
catch (Exception ex)
91+
{
92+
_logger.LogError(ex, "Failed to create entry in FileManager.");
93+
return BadRequest(new { error = ex.Message });
94+
}
95+
}
96+
97+
// Handles renaming files or folders in Azure Blob Storage
98+
[HttpPost]
99+
public async Task<IActionResult> FileManager_Update([FromForm] FileManagerUpdateRequest request)
100+
{
101+
try
102+
{
103+
var targetPath = request.Path;
104+
var newName = request.Name;
105+
106+
if (string.IsNullOrEmpty(targetPath) || string.IsNullOrEmpty(newName))
107+
{
108+
return BadRequest(new { error = "Path and name are required for rename operation" });
109+
}
110+
111+
// Rename is implemented by copying the blob to a new path and deleting the old one
112+
var result = await _fileManagerService.UpdateAsync(targetPath!, newName!);
113+
return Json(result);
114+
}
115+
catch (Exception ex)
116+
{
117+
_logger.LogError(ex, "Failed to rename FileManager entry.");
118+
return BadRequest(new { error = ex.Message });
119+
}
120+
}
121+
122+
// Handles deleting files or folders from Azure Blob Storage
123+
[HttpPost]
124+
public async Task<IActionResult> FileManager_Destroy([FromForm] FileManagerDestroyRequest request)
125+
{
126+
try
127+
{
128+
// Parse the request to extract the path of the item to delete
129+
var targetPath = ResolveTargetPath(request);
130+
if (string.IsNullOrEmpty(targetPath))
131+
{
132+
return BadRequest(new { error = "No target path provided for deletion." });
133+
}
134+
135+
await _fileManagerService.DeleteAsync(targetPath);
136+
return Json(Array.Empty<object>());
137+
}
138+
catch (Exception ex)
139+
{
140+
_logger.LogError(ex, "Failed to delete FileManager entry.");
141+
return BadRequest(new { error = ex.Message });
142+
}
143+
}
144+
145+
// Handles file uploads to Azure Blob Storage
146+
[HttpPost]
147+
public async Task<IActionResult> FileManager_Upload([FromForm] FileManagerUploadRequest request)
148+
{
149+
try
150+
{
151+
if (request.File == null || request.File.Length == 0)
152+
{
153+
return BadRequest(new { error = "No file uploaded" });
154+
}
155+
156+
// Normalize the target path and upload the file to the blob container
157+
var resolvedTarget = NormalizeUploadTarget(request);
158+
var result = await _fileManagerService.UploadAsync(resolvedTarget, request.File);
159+
return Json(result);
160+
}
161+
catch (Exception ex)
162+
{
163+
_logger.LogError(ex, "Failed to upload file.");
164+
return BadRequest(new { error = ex.Message });
165+
}
166+
}
167+
168+
private static string NormalizePath(string? target)
169+
{
170+
if (string.IsNullOrWhiteSpace(target))
171+
{
172+
return string.Empty;
173+
}
174+
175+
return target.Trim('/');
176+
}
177+
178+
private static string NormalizeUploadTarget(FileManagerUploadRequest request)
179+
{
180+
var resolvedTarget = request.Target ?? request.Path ?? string.Empty;
181+
return NormalizePath(resolvedTarget);
182+
}
183+
184+
private static string? ResolveTargetPath(FileManagerDestroyRequest request)
185+
{
186+
var targetPath = request.Path ?? request.Target ?? request.Name;
187+
188+
if (!string.IsNullOrWhiteSpace(targetPath))
189+
{
190+
return targetPath;
191+
}
192+
193+
if (string.IsNullOrWhiteSpace(request.Models))
194+
{
195+
return null;
196+
}
197+
198+
try
199+
{
200+
var modelsArray = JsonSerializer.Deserialize<JsonElement[]>(request.Models);
201+
if (modelsArray is { Length: > 0 })
202+
{
203+
var firstModel = modelsArray[0];
204+
if (firstModel.TryGetProperty("path", out var pathElement))
205+
{
206+
return pathElement.GetString();
207+
}
208+
}
209+
}
210+
catch
211+
{
212+
// Swallow JSON parsing errors and fall back to form values
213+
}
214+
215+
return null;
216+
}
217+
}
218+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.IO;
2+
3+
namespace KendoUI.FileManager.BlobStorage.Helpers
4+
{
5+
internal static class BlobPathHelper
6+
{
7+
public static string BuildPrefix(string? target)
8+
{
9+
return string.IsNullOrWhiteSpace(target)
10+
? string.Empty
11+
: target.TrimEnd('/') + "/";
12+
}
13+
14+
public static string CombinePath(string? target, string name)
15+
{
16+
var prefix = BuildPrefix(target);
17+
return string.IsNullOrEmpty(prefix) ? name : prefix + name;
18+
}
19+
20+
public static string EnsureTrailingSlash(string? path)
21+
{
22+
if (string.IsNullOrEmpty(path))
23+
{
24+
return path ?? string.Empty;
25+
}
26+
27+
return path.EndsWith('/') ? path : path + "/";
28+
}
29+
30+
public static bool ShouldTreatAsDirectory(string name, string? isDirectoryFlag, int entry)
31+
{
32+
if (!string.IsNullOrEmpty(isDirectoryFlag) && bool.TryParse(isDirectoryFlag, out var isDirectory) && isDirectory)
33+
{
34+
return true;
35+
}
36+
37+
if (entry == 1)
38+
{
39+
return true;
40+
}
41+
42+
return !HasExtension(name);
43+
}
44+
45+
public static string BuildNewPath(string targetPath, string newName, bool isDirectory)
46+
{
47+
var lastSlashIndex = targetPath.LastIndexOf('/');
48+
49+
if (isDirectory)
50+
{
51+
return lastSlashIndex >= 0
52+
? targetPath.Substring(0, lastSlashIndex + 1) + newName
53+
: newName;
54+
}
55+
56+
var originalExtension = Path.GetExtension(targetPath);
57+
var newExtension = Path.GetExtension(newName);
58+
59+
if (string.IsNullOrEmpty(newExtension) && !string.IsNullOrEmpty(originalExtension))
60+
{
61+
newName += originalExtension;
62+
}
63+
64+
return lastSlashIndex >= 0
65+
? targetPath.Substring(0, lastSlashIndex + 1) + newName
66+
: newName;
67+
}
68+
69+
public static bool HasExtension(string? name)
70+
{
71+
return !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(Path.GetExtension(name));
72+
}
73+
}
74+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Azure.Storage.Blobs" Version="12.26.0" />
11+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.0" />
12+
<PackageReference Include="Telerik.UI.for.AspNet.Core" Version="2025.4.1111" />
13+
</ItemGroup>
14+
15+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
16+
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<Compile Remove="Templates\**" />
21+
<Content Remove="Templates\**" />
22+
<EmbeddedResource Remove="Templates\**" />
23+
<None Remove="Templates\**" />
24+
</ItemGroup>
25+
26+
<ProjectExtensions>
27+
<VisualStudio>
28+
<UserProperties UseCdnSupport="True" />
29+
</VisualStudio>
30+
</ProjectExtensions>
31+
32+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System.Linq;
3+
4+
namespace KendoUI.FileManager.BlobStorage.Models
5+
{
6+
public sealed class FileManagerCreateContext
7+
{
8+
public IFormFile? UploadedFile { get; init; }
9+
public string? SourcePath { get; init; }
10+
public string? Extension { get; init; }
11+
public string? IsDirectoryFlag { get; init; }
12+
13+
public static FileManagerCreateContext FromRequest(FileManagerCreateRequest request)
14+
{
15+
if (request is null)
16+
{
17+
throw new ArgumentNullException(nameof(request));
18+
}
19+
20+
return new FileManagerCreateContext
21+
{
22+
UploadedFile = request.Files?.FirstOrDefault(),
23+
SourcePath = request.Path ??
24+
request.Source ??
25+
request.SourcePath,
26+
Extension = request.Extension,
27+
IsDirectoryFlag = request.IsDirectoryFlag
28+
};
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)