forked from gordon-matt/elFinder.NetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootVolume.cs
More file actions
192 lines (166 loc) · 6.96 KB
/
Copy pathRootVolume.cs
File metadata and controls
192 lines (166 loc) · 6.96 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using elFinder.NetCore.Drawing;
using elFinder.NetCore.Drivers;
using elFinder.NetCore.Helpers;
namespace elFinder.NetCore
{
/// <summary>
/// Represents a root of file system
/// </summary>
public class RootVolume
{
public RootVolume(string rootDirectory, string url, string thumbnailsUrl = null)
{
if (rootDirectory == null)
{
throw new ArgumentNullException("rootDirectory", "Root directory cannot be null");
}
Alias = Path.GetFileNameWithoutExtension(rootDirectory);
RootDirectory = rootDirectory;
Url = url;
UploadOverwrite = true;
ThumbnailSize = 48;
PictureEditor = new DefaultPictureEditor();
// https://github.com/EvgenNoskov/Elfinder.NET/blob/fb19f17a3682ed81cadcfea978dcce575806eebd/docs/Documentation.md
if (!string.IsNullOrEmpty(thumbnailsUrl))
{
ThumbnailUrl = thumbnailsUrl;
}
ThumbnailDirectory = string.Concat(rootDirectory, "/", ".tmb");
}
/// <summary>
/// Get or sets alias for root. If not set will use directory name of path
/// </summary>
public string Alias { get; set; }
/// <summary>
/// Get or sets if root is locked (user can't remove, rename or delete files or subdirectories)
/// </summary>
public bool IsLocked { get; }
/// <summary>
/// Get or sets if root for read only (users can't change file)
/// </summary>
public bool IsReadOnly { get; set; }
/// <summary>
/// Get or sets if user can only show files (and cannot download).
/// Note: if you set url property, than users can access to directory by the provided url
/// </summary>
public bool IsShowOnly { get; }
/// <summary>
/// Gets or sets a list of root subfolders that should be locked (user can't remove, rename)
/// </summary>
public List<string> LockedFolders { get; }
/// <summary>
/// Get or sets maximum upload file size. This size is per files in bytes.
/// Note: you still to configure maxupload limits in web.config for whole application
/// </summary>
public int? MaxUploadSize { get; set; }
/// <summary>
/// Get or sets maximum upload file size. This size is per files in kb.
/// Note: you still to configure maxupload limits in web.config for whole application
/// </summary>
public double? MaxUploadSizeInKb
{
get { return MaxUploadSize.HasValue ? (double?)(MaxUploadSize.Value / 1024.0) : null; }
set { MaxUploadSize = value.HasValue ? (int?)(value * 1024) : null; }
}
/// <summary>
/// Get or sets maximum upload file size. This size is per files in Mb.
/// Note: you still to configure maxupload limits in web.config for whole application
/// </summary>
public double? MaxUploadSizeInMb
{
get { return MaxUploadSizeInKb.HasValue ? (double?)(MaxUploadSizeInKb.Value / 1024.0) : null; }
set { MaxUploadSizeInKb = value.HasValue ? (int?)(value * 1024) : null; }
}
/// <summary>
/// Gets the picture editor for this volume
/// </summary>
public IPictureEditor PictureEditor { get; }
/// <summary>
/// Get or sets a directory which is root
/// </summary>
public string RootDirectory { get; }
/// <summary>
/// Get or sets a subfolder of root diretory, which will be start
/// </summary>
public string StartDirectory { get; }
/// <summary>
/// Get ot sets thumbnals directory
/// </summary>
public string ThumbnailDirectory { get; }
/// <summary>
/// Get or sets thumbnails size
/// </summary>
public int ThumbnailSize { get; }
/// <summary>
/// Get ot sets thumbnails url
/// </summary>
public string ThumbnailUrl { get; }
/// <summary>
/// Get or sets if files on upload will replace or give them new names. true - replace old files, false give new names like original_name-number.ext
/// </summary>
public bool UploadOverwrite { get; }
/// <summary>
/// Get or sets url that points to path directory (also called 'root URL').
/// </summary>
public string Url { get; }
/// <summary>
/// Gets a autogenerated prefix of root
/// </summary>
public string VolumeId { get; set; }
public bool CanCreateThumbnail(IFile input)
{
return ThumbnailUrl != null && PictureEditor.CanProcessFile(input.Extension);
}
public async Task<string> GenerateThumbHash(IFile originalImage)
{
if (ThumbnailDirectory == null)
{
string thumbName = Path.GetFileNameWithoutExtension(originalImage.Name) + "_" + await Utils.GetFileMd5(originalImage) + originalImage.Extension;
string relativePath = originalImage.DirectoryName.Substring(RootDirectory.Length);
return VolumeId + Utils.EncodePath($"{relativePath}/{thumbName}");
}
else
{
string thumbPath = await GenerateThumbPath(originalImage);
string relativePath = thumbPath.Substring(ThumbnailDirectory.Length);
return VolumeId + Utils.EncodePath(relativePath);
}
}
public async Task<string> GenerateThumbPath(IFile originalImage)
{
if (ThumbnailDirectory == null || !CanCreateThumbnail(originalImage))
{
return null;
}
string relativePath = originalImage.FullName.Substring(RootDirectory.Length);
string thumbDir = GetDirectoryName(string.Concat(ThumbnailDirectory + relativePath));
string thumbName = Path.GetFileNameWithoutExtension(originalImage.Name) + "_" + await Utils.GetFileMd5(originalImage) + originalImage.Extension;
return string.Concat(thumbDir, "/", thumbName);
}
public string GenerateThumbPath(IDirectory originalDirectory)
{
if (ThumbnailDirectory == null)
{
return null;
}
string relativePath = originalDirectory.FullName.Substring(RootDirectory.Length);
return ThumbnailDirectory + relativePath;
}
private string GetDirectoryName(string file)
{
int length = file.Length;
int startIndex = length;
while (--startIndex >= 0)
{
char ch = file[startIndex];
if (ch == '/' || ch == '\\')
return file.Substring(0, startIndex);
}
return string.Empty;
}
}
}