/
AzureProviderController.cs
137 lines (132 loc) · 7.02 KB
/
AzureProviderController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Syncfusion.EJ2.FileManager.Base;
using Syncfusion.EJ2.FileManager.AzureFileProvider;
using System.Collections.Generic;
namespace EJ2AzureASPCoreFileProvider.Controllers
{
[Route("api/[controller]")]
[EnableCors("AllowAllOrigins")]
public class AzureProviderController : Controller
{
public AzureFileProvider operation;
public string blobPath { get; set; }
public string filePath { get; set; }
public AzureProviderController(IWebHostEnvironment hostingEnvironment)
{
this.operation = new AzureFileProvider();
blobPath = "<--blobPath-->";
filePath = "<--filePath-->";
blobPath = blobPath.Replace("../", "");
filePath = filePath.Replace("../", "");
blobPath = (blobPath.Substring(blobPath.Length - 1) != "/") ? blobPath + "/" : blobPath.TrimEnd(new[] { '/', '\\' }) + "/";
filePath = (filePath.Substring(filePath.Length - 1) == "/") ? filePath.TrimEnd(new[] { '/', '\\' }) : filePath;
this.operation.SetBlobContainer(blobPath, filePath);
this.operation.RegisterAzure("<--accountName-->", "<--accountKey-->", "<--blobName-->");
//----------
//For example
//this.operation.setBlobContainer("https://azure_service_account.blob.core.windows.net/files/", "https://azure_service_account.blob.core.windows.net/files/Files");
//this.operation.RegisterAzure("azure_service_account", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "files");
//---------
}
[Route("AzureFileOperations")]
public object AzureFileOperations([FromBody] FileManagerDirectoryContent args)
{
if (args.Path != "")
{
string startPath = blobPath;
string originalPath = (filePath).Replace(startPath, "");
//-----------------
//For example
//string startPath = "https://azure_service_account.blob.core.windows.net/files/";
//string originalPath = ("https://azure_service_account.blob.core.windows.net/files/Files").Replace(startPath, "");
//-------------------
args.Path = !args.Path.Contains(originalPath) ? (originalPath + args.Path).Replace("//", "/") : (args.Path).Replace("//", "/");
args.TargetPath = (originalPath + args.TargetPath).Replace("//", "/");
}
switch (args.Action)
{
case "read":
// Reads the file(s) or folder(s) from the given path.
return Json(this.ToCamelCase(this.operation.GetFiles(args.Path, args.ShowHiddenItems, args.Data)));
case "delete":
// Deletes the selected file(s) or folder(s) from the given path.
return this.ToCamelCase(this.operation.Delete(args.Path, args.Names, args.Data));
case "details":
// Gets the details of the selected file(s) or folder(s).
return this.ToCamelCase(this.operation.Details(args.Path, args.Names, args.Data));
case "create":
// Creates a new folder in a given path.
return this.ToCamelCase(this.operation.Create(args.Path, args.Name, args.Data));
case "search":
// Gets the list of file(s) or folder(s) from a given path based on the searched key string.
return this.ToCamelCase(this.operation.Search(args.Path, args.SearchString, args.ShowHiddenItems, args.CaseSensitive, args.Data));
case "rename":
// Renames a file or folder.
return this.ToCamelCase(this.operation.Rename(args.Path, args.Name, args.NewName, false, args.ShowFileExtension, args.Data));
case "copy":
// Copies the selected file(s) or folder(s) from a path and then pastes them into a given target path.
return this.ToCamelCase(this.operation.Copy(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData, args.Data));
case "move":
// Cuts the selected file(s) or folder(s) from a path and then pastes them into a given target path.
return this.ToCamelCase(this.operation.Move(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData, args.Data));
}
return null;
}
public string ToCamelCase(object userData)
{
return JsonConvert.SerializeObject(userData, new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
});
}
// Uploads the file(s) into a specified path
[Route("AzureUpload")]
public ActionResult AzureUpload(FileManagerDirectoryContent args)
{
if (args.Path != "")
{
string startPath = blobPath;
string originalPath = (filePath).Replace(startPath, "");
args.Path = (originalPath + args.Path).Replace("//", "/");
//----------------------
//For example
//string startPath = "https://azure_service_account.blob.core.windows.net/files/";
//string originalPath = ("https://azure_service_account.blob.core.windows.net/files/Files").Replace(startPath, "");
//args.Path = (originalPath + args.Path).Replace("//", "/");
//----------------------
}
FileManagerResponse uploadResponse = operation.Upload(args.Path, args.UploadFiles, args.Action, args.Data);
if (uploadResponse.Error != null)
{
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = Convert.ToInt32(uploadResponse.Error.Code);
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = uploadResponse.Error.Message;
}
return Json("");
}
// Downloads the selected file(s) and folder(s)
[Route("AzureDownload")]
public object AzureDownload(string downloadInput)
{
FileManagerDirectoryContent args = JsonConvert.DeserializeObject<FileManagerDirectoryContent>(downloadInput);
return operation.Download(args.Path, args.Names, args.Data);
}
// Gets the image(s) from the given path
[Route("AzureGetImage")]
public IActionResult AzureGetImage(FileManagerDirectoryContent args)
{
return this.operation.GetImage(args.Path, args.Id, true, null, args.Data);
}
}
}