forked from gordon-matt/elFinder.NetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseModel.cs
More file actions
162 lines (145 loc) · 6.2 KB
/
Copy pathBaseModel.cs
File metadata and controls
162 lines (145 loc) · 6.2 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
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using elFinder.NetCore.Drivers;
using elFinder.NetCore.Helpers;
using Newtonsoft.Json;
namespace elFinder.NetCore.Models
{
public abstract class BaseModel
{
protected static readonly DateTime unixOrigin = new DateTime(1970, 1, 1, 0, 0, 0);
/// <summary>
/// Name of file/dir. Required
/// </summary>
[JsonProperty("name")]
public string Name { get; protected set; }
/// <summary>
/// Hash of current file/dir path, first symbol must be letter, symbols before _underline_ - volume id, Required.
/// </summary>
[JsonProperty("hash")]
public string Hash { get; protected set; }
/// <summary>
/// mime type. Required.
/// </summary>
[JsonProperty("mime")]
public string Mime { get; protected set; }
/// <summary>
/// file modification time in unix timestamp. Required.
/// </summary>
[JsonProperty("ts")]
public long UnixTimeStamp { get; protected set; }
/// <summary>
/// file size in bytes
/// </summary>
[JsonProperty("size")]
public long Size { get; protected set; }
/// <summary>
/// is readable
/// </summary>
[JsonProperty("read")]
public byte Read { get; protected set; }
/// <summary>
/// is writable
/// </summary>
[JsonProperty("write")]
public byte Write { get; protected set; }
/// <summary>
/// is file locked. If locked that object cannot be deleted and renamed
/// </summary>
[JsonProperty("locked")]
public byte Locked { get; protected set; }
public static async Task<BaseModel> CreateAsync(IDriver driver, IFile file, RootVolume volume)
{
if (file == null) throw new ArgumentNullException("file");
if (volume == null) throw new ArgumentNullException("volume");
string parentPath = file.DirectoryName.Substring(volume.RootDirectory.Length);
string relativePath = file.FullName.Substring(volume.RootDirectory.Length);
FileModel response;
if (volume.CanCreateThumbnail(file))
{
using (var stream = await file.OpenReadAsync())
{
var dim = volume.PictureEditor.ImageSize(stream);
response = new ImageModel
{
Thumbnail = await volume.GenerateThumbHashAsync(file),
Dimension = $"{dim.Width}x{dim.Height}"
};
}
}
else
{
response = new FileModel();
}
response.Read = 1;
response.Write = volume.IsReadOnly ? (byte)0 : (byte)1;
response.Locked = ((volume.LockedFolders != null && volume.LockedFolders.Any(f => f == file.Directory.Name)) || volume.IsLocked) ? (byte)1 : (byte)0;
response.Name = file.Name;
response.Size = await file.LengthAsync;
response.UnixTimeStamp = (long)(await file.LastWriteTimeUtcAsync - unixOrigin).TotalSeconds;
response.Mime = MimeHelper.GetMimeType(file.Extension);
response.Hash = volume.VolumeId + HttpEncoder.EncodePath(relativePath);
response.ParentHash = volume.VolumeId + HttpEncoder.EncodePath(parentPath.Length > 0 ? parentPath : file.Directory.Name);
return response;
}
public static async Task<BaseModel> CreateAsync(IDriver driver, IDirectory directory, RootVolume volume)
{
if (directory == null)
{
throw new ArgumentNullException("directory");
}
if (volume == null)
{
throw new ArgumentNullException("volume");
}
if (volume.RootDirectory == directory.FullName)
{
bool hasSubdirs = false;
var subdirs = await directory.GetDirectoriesAsync();
foreach (var item in subdirs)
{
if (!item.Attributes.HasFlag(FileAttributes.Hidden))
{
hasSubdirs = true;
break;
}
}
var response = new RootModel
{
Mime = "directory",
Dirs = hasSubdirs ? (byte)1 : (byte)0,
Hash = volume.VolumeId + HttpEncoder.EncodePath(directory.Name),
Read = 1,
Write = volume.IsReadOnly ? (byte)0 : (byte)1,
Locked = volume.IsLocked ? (byte)1 : (byte)0,
Name = volume.Alias,
Size = 0,
UnixTimeStamp = (long)(DateTime.UtcNow - unixOrigin).TotalSeconds,
VolumeId = volume.VolumeId
};
return response;
}
else
{
string parentPath = directory.Parent.FullName.Substring(volume.RootDirectory.Length);
string relativePath = directory.FullName.Substring(volume.RootDirectory.Length).TrimEnd(Path.DirectorySeparatorChar);
var response = new DirectoryModel
{
Mime = "directory",
ContainsChildDirs = (await directory.GetDirectoriesAsync()).Count() > 0 ? (byte)1 : (byte)0,
Hash = volume.VolumeId + HttpEncoder.EncodePath(relativePath),
Read = 1,
Write = volume.IsReadOnly ? (byte)0 : (byte)1,
Locked = ((volume.LockedFolders != null && volume.LockedFolders.Any(f => f == directory.Name)) || volume.IsLocked) ? (byte)1 : (byte)0,
Size = 0,
Name = directory.Name,
UnixTimeStamp = (long)(await directory.LastWriteTimeUtcAsync - unixOrigin).TotalSeconds,
ParentHash = volume.VolumeId + HttpEncoder.EncodePath(parentPath.Length > 0 ? parentPath : directory.Parent.Name)
};
return response;
}
}
}
}