예제 #1
0
        public Uri GetPreSignedUri(string domain, string path, TimeSpan expire, IEnumerable<string> headers)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (string.IsNullOrEmpty(_tenant) && IsSupportInternalUri)
            {
                return GetInternalUri(domain, path, expire, headers);
            }

            var headerAttr = string.Empty;
            if (headers != null)
            {
                headerAttr = string.Join("&", headers.Select(HttpUtility.UrlEncode));
            }

            if (expire == TimeSpan.Zero || expire == TimeSpan.MinValue || expire == TimeSpan.MaxValue)
            {
                expire = GetExpire(domain);
            }

            var query = string.Empty;
            if (expire != TimeSpan.Zero && expire != TimeSpan.MinValue && expire != TimeSpan.MaxValue)
            {
                var expireString = expire.TotalMinutes.ToString(CultureInfo.InvariantCulture);

                int currentTenantId;
                var currentTenant = TenantManager.GetCurrentTenant(false);
                if (currentTenant != null)
                {
                    currentTenantId = currentTenant.TenantId;
                }
                else if (!TenantPath.TryGetTenant(_tenant, out currentTenantId))
                {
                    currentTenantId = 0;
                }

                var auth = EmailValidationKeyProvider.GetEmailKey(currentTenantId, path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + "." + headerAttr + "." + expireString);
                query = string.Format("{0}{1}={2}&{3}={4}",
                                      path.Contains("?") ? "&" : "?",
                                      Constants.QUERY_EXPIRE,
                                      expireString,
                                      Constants.QUERY_AUTH,
                                      auth);
            }

            if (!string.IsNullOrEmpty(headerAttr))
            {
                query += string.Format("{0}{1}={2}",
                                       query.Contains("?") ? "&" : "?",
                                       Constants.QUERY_HEADER,
                                       HttpUtility.UrlEncode(headerAttr));
            }

            var tenant = _tenant.Trim('/');
            var vpath = PathUtils.ResolveVirtualPath(_modulename, domain);
            vpath = PathUtils.ResolveVirtualPath(vpath, false);
            vpath = string.Format(vpath, tenant);
            var virtualPath = new Uri(vpath + "/", UriKind.RelativeOrAbsolute);

            var uri = virtualPath.IsAbsoluteUri ?
                          new MonoUri(virtualPath, virtualPath.LocalPath.TrimEnd('/') + EnsureLeadingSlash(path.Replace('\\', '/')) + query) :
                          new MonoUri(virtualPath.ToString().TrimEnd('/') + EnsureLeadingSlash(path.Replace('\\', '/')) + query, UriKind.Relative);

            return uri;
        }
        public static void InitializeHttpHandlers(this IEndpointRouteBuilder builder, string config = null)
        {
            //TODO:
            //if (!HostingEnvironment.IsHosted)
            //{
            //    throw new InvalidOperationException("Application not hosted.");
            //}

            var section = builder.ServiceProvider.GetService <Configuration.Storage>();

            if (section != null)
            {
                //old scheme
                var discHandler = section.GetHandler("disc");
                if (discHandler != null && section.Module != null)
                {
                    var props = discHandler.Property != null?discHandler.Property.ToDictionary(r => r.Name, r => r.Value) : new Dictionary <string, string>();

                    foreach (var m in section.Module.Where(m => m.Type == "disc"))
                    {
                        if (m.Path.Contains(Constants.STORAGE_ROOT_PARAM))
                        {
                            builder.RegisterDiscDataHandler(
                                PathUtils.ResolveVirtualPath(m.VirtualPath),
                                PathUtils.ResolvePhysicalPath(m.Path, props),
                                m.Public);
                        }

                        if (m.Domain != null)
                        {
                            foreach (var d in m.Domain.Where(d => (d.Type == "disc" || string.IsNullOrEmpty(d.Type)) && d.Path.Contains(Constants.STORAGE_ROOT_PARAM)))
                            {
                                builder.RegisterDiscDataHandler(
                                    PathUtils.ResolveVirtualPath(d.VirtualPath),
                                    PathUtils.ResolvePhysicalPath(d.Path, props));
                            }
                        }
                    }
                }

                //new scheme
                if (section.Module != null)
                {
                    foreach (var m in section.Module)
                    {
                        //todo: add path criterion
                        if (m.Type == "disc" || !m.Public || m.Path.Contains(Constants.STORAGE_ROOT_PARAM))
                        {
                            builder.RegisterStorageHandler(
                                m.Name,
                                string.Empty,
                                m.Public);
                        }

                        //todo: add path criterion
                        if (m.Domain != null)
                        {
                            foreach (var d in m.Domain.Where(d => d.Path.Contains(Constants.STORAGE_ROOT_PARAM)))
                            {
                                builder.RegisterStorageHandler(
                                    m.Name,
                                    d.Name,
                                    d.Public);
                            }
                        }
                    }
                }
            }
        }