fixed StorageHandler class

This commit is contained in:
Alexey Bannov 2022-12-22 18:48:30 +03:00
parent e47d45c7fd
commit f687c16a6b
8 changed files with 43 additions and 217 deletions

View File

@ -348,21 +348,7 @@ public abstract class BaseStorage : IDataStore
return "/" + str.TrimStart('/'); return "/" + str.TrimStart('/');
} }
protected abstract Task<DateTime> GetLastModificationDateAsync(string domain, string path); public abstract Task<string> GetFileEtagAsync(string domain, string path);
public async Task<(bool success, string etag)> TryGetFileEtagAsync(string domain, string path)
{
var etag = "";
if (Cache)
{
var lastModificationDate = await GetLastModificationDateAsync(domain, path);
etag = '"' + lastModificationDate.Ticks.ToString("X8", CultureInfo.InvariantCulture) + '"';
return (true, etag);
}
return (false, etag);
}
internal class MonoUri : Uri internal class MonoUri : Uri
{ {

View File

@ -43,7 +43,6 @@ public class DiscDataStore : BaseStorage
Tenant = tenant; Tenant = tenant;
//Fill map path //Fill map path
Modulename = moduleConfig.Name; Modulename = moduleConfig.Name;
Cache = moduleConfig.Cache;
DataList = new DataList(moduleConfig); DataList = new DataList(moduleConfig);
foreach (var domain in moduleConfig.Domain) foreach (var domain in moduleConfig.Domain)
@ -762,14 +761,14 @@ public class DiscDataStore : BaseStorage
} }
} }
protected override Task<DateTime> GetLastModificationDateAsync(string domain, string path) public override Task<string> GetFileEtagAsync(string domain, string path)
{ {
ArgumentNullException.ThrowIfNull(path); ArgumentNullException.ThrowIfNull(path);
var target = GetTarget(domain, path); var target = GetTarget(domain, path);
var lastModificationDate = File.Exists(target) ? File.GetLastWriteTimeUtc(target) : throw new FileNotFoundException("File not found" + target);
var etag = '"' + lastModificationDate.Ticks.ToString("X8", CultureInfo.InvariantCulture) + '"';
return File.Exists(target) return Task.FromResult(etag);
? Task.FromResult(File.GetLastWriteTimeUtc(target))
: throw new FileNotFoundException("File not found" + target);
} }
} }

View File

@ -60,7 +60,6 @@ public class GoogleCloudStorage : BaseStorage
if (moduleConfig != null) if (moduleConfig != null)
{ {
Modulename = moduleConfig.Name; Modulename = moduleConfig.Name;
Cache = moduleConfig.Cache;
DataList = new DataList(moduleConfig); DataList = new DataList(moduleConfig);
DomainsExpires = moduleConfig.Domain.Where(x => x.Expires != TimeSpan.Zero).ToDictionary(x => x.Name, y => y.Expires); DomainsExpires = moduleConfig.Domain.Where(x => x.Expires != TimeSpan.Zero).ToDictionary(x => x.Name, y => y.Expires);
@ -73,7 +72,6 @@ public class GoogleCloudStorage : BaseStorage
else else
{ {
Modulename = string.Empty; Modulename = string.Empty;
Cache = false;
DataList = null; DataList = null;
DomainsExpires = new Dictionary<string, TimeSpan> { { string.Empty, TimeSpan.Zero } }; DomainsExpires = new Dictionary<string, TimeSpan> { { string.Empty, TimeSpan.Zero } };
@ -856,13 +854,17 @@ public class GoogleCloudStorage : BaseStorage
return _moduleAcl; return _moduleAcl;
} }
protected override async Task<DateTime> GetLastModificationDateAsync(string domain, string path) public override async Task<string> GetFileEtagAsync(string domain, string path)
{ {
var storage = GetStorage(); var storage = GetStorage();
var objectName = MakePath(domain, path); var objectName = MakePath(domain, path);
var obj = await storage.GetObjectAsync(_bucket, objectName); var obj = await storage.GetObjectAsync(_bucket, objectName);
return obj == null ? throw new FileNotFoundException("File not found" + objectName) : obj.Updated ?? obj.TimeCreated ?? DateTime.MinValue; var lastModificationDate = obj == null ? throw new FileNotFoundException("File not found" + objectName) : obj.Updated ?? obj.TimeCreated ?? DateTime.MinValue;
var etag = '"' + lastModificationDate.Ticks.ToString("X8", CultureInfo.InvariantCulture) + '"';
return etag;
} }
} }

View File

@ -310,5 +310,5 @@ public interface IDataStore
string GetPostParams(string domain, string directoryPath, long maxUploadSize, string contentType, string GetPostParams(string domain, string directoryPath, long maxUploadSize, string contentType,
string contentDisposition); string contentDisposition);
Task<(bool success, string etag)> TryGetFileEtagAsync(string domain, string path); Task<string> GetFileEtagAsync(string domain, string path);
} }

View File

@ -69,7 +69,6 @@ public class RackspaceCloudStorage : BaseStorage
if (moduleConfig != null) if (moduleConfig != null)
{ {
Modulename = moduleConfig.Name; Modulename = moduleConfig.Name;
Cache = moduleConfig.Cache;
DataList = new DataList(moduleConfig); DataList = new DataList(moduleConfig);
_domains.AddRange(moduleConfig.Domain.Select(x => $"{x.Name}/")); _domains.AddRange(moduleConfig.Domain.Select(x => $"{x.Name}/"));
DomainsExpires = moduleConfig.Domain.Where(x => x.Expires != TimeSpan.Zero).ToDictionary(x => x.Name, y => y.Expires); DomainsExpires = moduleConfig.Domain.Where(x => x.Expires != TimeSpan.Zero).ToDictionary(x => x.Name, y => y.Expires);
@ -80,7 +79,6 @@ public class RackspaceCloudStorage : BaseStorage
else else
{ {
Modulename = string.Empty; Modulename = string.Empty;
Cache = false;
DataList = null; DataList = null;
DomainsExpires = new Dictionary<string, TimeSpan> { { string.Empty, TimeSpan.Zero } }; DomainsExpires = new Dictionary<string, TimeSpan> { { string.Empty, TimeSpan.Zero } };
_domainsAcl = new Dictionary<string, ACL>(); _domainsAcl = new Dictionary<string, ACL>();
@ -754,13 +752,17 @@ public class RackspaceCloudStorage : BaseStorage
return _moduleAcl; return _moduleAcl;
} }
protected override Task<DateTime> GetLastModificationDateAsync(string domain, string path) public override Task<string> GetFileEtagAsync(string domain, string path)
{ {
var client = GetClient(); var client = GetClient();
var prefix = MakePath(domain, path); var prefix = MakePath(domain, path);
var obj = client.ListObjects(_private_container, null, null, null, prefix, _region).FirstOrDefault(); var obj = client.ListObjects(_private_container, null, null, null, prefix, _region).FirstOrDefault();
return obj == null ? throw new FileNotFoundException("File not found" + prefix) : Task.FromResult(obj.LastModified.UtcDateTime); var lastModificationDate = obj == null ? throw new FileNotFoundException("File not found" + prefix) : obj.LastModified.UtcDateTime;
var etag = '"' + lastModificationDate.Ticks.ToString("X8", CultureInfo.InvariantCulture) + '"';
return Task.FromResult(etag);
} }
} }

View File

@ -871,7 +871,6 @@ public class S3Storage : BaseStorage
if (moduleConfig != null) if (moduleConfig != null)
{ {
Modulename = moduleConfig.Name; Modulename = moduleConfig.Name;
Cache = moduleConfig.Cache;
DataList = new DataList(moduleConfig); DataList = new DataList(moduleConfig);
_domains.AddRange(moduleConfig.Domain.Select(x => $"{x.Name}/")); _domains.AddRange(moduleConfig.Domain.Select(x => $"{x.Name}/"));
@ -885,7 +884,6 @@ public class S3Storage : BaseStorage
else else
{ {
Modulename = string.Empty; Modulename = string.Empty;
Cache = false;
DataList = null; DataList = null;
//Make expires //Make expires
@ -1445,7 +1443,7 @@ public class S3Storage : BaseStorage
return method; return method;
} }
protected override async Task<DateTime> GetLastModificationDateAsync(string domain, string path) public override async Task<string> GetFileEtagAsync(string domain, string path)
{ {
using var client = GetClient(); using var client = GetClient();
@ -1457,7 +1455,7 @@ public class S3Storage : BaseStorage
var el = await client.GetObjectMetadataAsync(getObjectMetadataRequest); var el = await client.GetObjectMetadataAsync(getObjectMetadataRequest);
return el.LastModified; return el.ETag;
} }
private enum EncryptionMethod private enum EncryptionMethod

View File

@ -85,7 +85,9 @@ public class StorageHandler
var headers = header.Length > 0 ? header.Split('&').Select(HttpUtility.UrlDecode) : Array.Empty<string>(); var headers = header.Length > 0 ? header.Split('&').Select(HttpUtility.UrlDecode) : Array.Empty<string>();
if (storage.IsSupportInternalUri) const int bigSize = 5 * 1024 * 1024;
if (storage.IsSupportInternalUri && bigSize < await storage.GetFileSizeAsync(_domain, path))
{ {
var uri = await storage.GetInternalUriAsync(_domain, path, TimeSpan.FromMinutes(15), headers); var uri = await storage.GetInternalUriAsync(_domain, path, TimeSpan.FromMinutes(15), headers);
@ -97,21 +99,24 @@ public class StorageHandler
return; return;
} }
var FileEtagTuple = await storage.TryGetFileEtagAsync(_domain, path); if (!String.IsNullOrEmpty(context.Request.Query["_"]))
if (FileEtagTuple.success)
{ {
var etag = FileEtagTuple.etag; context.Response.Headers["Cache-Control"] = "public, max-age=31536000";
if (string.Equals(context.Request.Headers["If-None-Match"], etag))
{
context.Response.StatusCode = (int)HttpStatusCode.NotModified;
return;
}
context.Response.Headers.ETag = etag;
} }
var etag = await storage.GetFileEtagAsync(_domain, path);
if (string.Equals(context.Request.Headers["If-None-Match"], etag))
{
context.Response.StatusCode = (int)HttpStatusCode.NotModified;
return;
}
context.Response.Headers.ETag = etag;
string encoding = null; string encoding = null;
if (storage is DiscDataStore && await storage.IsFileAsync(_domain, path + ".gz")) if (storage is DiscDataStore && await storage.IsFileAsync(_domain, path + ".gz"))
{ {
path += ".gz"; path += ".gz";

View File

@ -19,38 +19,6 @@
} }
], ],
"module": [ "module": [
{
"name": "forum",
"data": "853B6EB9-73EE-438d-9B09-8FFEEDF36234",
"type": "disc",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Forum\\Data\\attachments",
"virtualpath": "~/products/community/modules/forum/data/attachments",
"expires": "0:10:0"
},
{
"name": "bookmarking",
"data": "00000000-0000-0000-0000-000000000000",
"type": "disc",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Bookmarking\\Data\\images",
"virtualpath": "~/products/community/modules/bookmarking/data/images"
},
{
"name": "wiki",
"data": "742CF945-CBBC-4a57-82D6-1600A12CF8CA",
"type": "disc",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Wiki\\Data\\files",
"virtualpath": "~/products/community/modules/wiki/data/files",
"expires": "0:10:0",
"domain": [
{
"name": "temp",
"visible": false,
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Wiki\\Data\\filestemp",
"virtualpath": "~/products/community/modules/wiki/data/filestemp"
}
]
},
{ {
"name": "files", "name": "files",
"data": "e67be73d-f9ae-4ce1-8fec-1880cb518cb4", "data": "e67be73d-f9ae-4ce1-8fec-1880cb518cb4",
@ -95,121 +63,6 @@
"disableMigrate": true, "disableMigrate": true,
"disableEncryption": true "disableEncryption": true
}, },
{
"name": "crm",
"data": "6743007C-6F95-4d20-8C88-A8601CE5E76D",
"type": "disc",
"path": "$STORAGE_ROOT\\Products\\CRM\\Data",
"virtualpath": "~/products/crm/data",
"domain": [
{
"name": "export",
"visible": false,
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\CRM\\Data\\{0}\\export",
"expires": "1.0:0:0"
},
{
"name": "temp",
"visible": false,
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\CRM\\Data\\{0}\\temp"
},
{
"name": "mail_messages",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\CRM\\Data\\{0}\\mail_messages"
},
{
"name": "voip",
"visible": true,
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\CRM\\Data\\{0}\\voip",
"virtualpath": "~/products/crm/data/{0}/voip",
"public": true
}
]
},
{
"name": "crm_template",
"visible": false,
"data": "00000000-0000-0000-0000-000000000000",
"type": "disc",
"path": "Products\\CRM\\DocStore",
"disableMigrate": true,
"disableEncryption": true
},
{
"name": "fckuploaders",
"count": false,
"data": "00000000-0000-0000-0000-000000000000",
"type": "disc",
"path": "$STORAGE_ROOT\\Studio\\htmleditorfiles",
"virtualpath": "~/data/shared/htmleditorfiles",
"domain": [
{
"name": "mail",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\addons\\mail\\Data\\htmleditorfiles",
"virtualpath": "~/addons/mail/data/htmleditorfiles"
},
{
"name": "forum",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Forum\\Data\\htmleditorfiles",
"virtualpath": "~/products/community/modules/forum/data/htmleditorfiles"
},
{
"name": "news",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\News\\Data\\htmleditorfiles",
"virtualpath": "~/products/community/modules/news/data/htmleditorfiles"
},
{
"name": "news_comments",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\News\\Data\\fckcomments",
"virtualpath": "~/products/community/modules/news/data/fckcomments"
},
{
"name": "blogs",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Blogs\\Data\\htmleditorfiles",
"virtualpath": "~/products/community/modules/blogs/data/htmleditorfiles"
},
{
"name": "blogs_comments",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Blogs\\Data\\fckcomments",
"virtualpath": "~/products/community/modules/blogs/data/fckcomments"
},
{
"name": "bookmarking_comments",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Bookmarking\\data\\fckcomments",
"virtualpath": "~/products/community/modules/bookmarking/data/fckcomments"
},
{
"name": "wiki_comments",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Community\\Modules\\Wiki\\Data\\fckcomments",
"virtualpath": "~/products/community/modules/wiki/data/fckcomments"
},
{
"name": "projects_comments",
"data": "00000000-0000-0000-0000-000000000000",
"path": "$STORAGE_ROOT\\Products\\Projects\\Data\\fckcomments",
"virtualpath": "~/products/projects/data/fckcomments"
}
]
},
{
"name": "talk",
"data": "BF88953E-3C43-4850-A3FB-B1E43AD53A3E",
"type": "disc",
"path": "$STORAGE_ROOT\\addons\\talk\\Data",
"virtualpath": "~/addons/talk/data"
},
{ {
"name": "certs", "name": "certs",
"visible": "false", "visible": "false",
@ -218,13 +71,6 @@
"appendTenantId": false, "appendTenantId": false,
"disableEncryption": true "disableEncryption": true
}, },
{
"name": "mailaggregator",
"data": "666ceac1-4532-4f8c-9cba-8f510eca2fd1",
"type": "disc",
"path": "$STORAGE_ROOT\\addons\\mail\\Data\\aggregator",
"virtualpath": "~/addons/mail/data/aggregator"
},
{ {
"name": "logo", "name": "logo",
"data": "00000000-0000-0000-0000-000000000000", "data": "00000000-0000-0000-0000-000000000000",
@ -272,7 +118,6 @@
"type": "disc", "type": "disc",
"path": "$STORAGE_ROOT\\Studio\\{0}\\userphotos", "path": "$STORAGE_ROOT\\Studio\\{0}\\userphotos",
"virtualpath": "~/studio/{0}/userphotos", "virtualpath": "~/studio/{0}/userphotos",
"cache": "true",
"domain": [ "domain": [
{ {
"name": "temp", "name": "temp",
@ -282,17 +127,6 @@
"virtualpath": "~/studio/{0}/userphotos/temp" "virtualpath": "~/studio/{0}/userphotos/temp"
} }
] ]
},
{
"name": "static_partnerdata",
"type": "disc",
"path": "App_Data\\static\\partnerdata",
"acl": "Read",
"virtualpath": "~/App_Data/static/partnerdata",
"appendTenantId": false,
"public": true,
"disableMigrate": true,
"disableEncryption": true
} }
] ]
} }