public void LocatorCRUD()
        {
            ILocator stubed = _mediaContext.Locators.FirstOrDefault();

            Assert.IsNotNull(stubed);
            Assert.IsNotNull(stubed.Asset);
            Assert.IsNotNull(stubed.AccessPolicy);

            IAsset        asset   = _mediaContext.Assets.Create("Test", AssetCreationOptions.None);
            IAccessPolicy policy  = _mediaContext.AccessPolicies.Create("Test", TimeSpan.FromDays(1), AccessPermissions.Read);
            ILocator      locator = _mediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset, policy);

            Assert.IsNotNull(locator.AccessPolicy);
            Assert.IsNotNull(locator.Asset);
            locator.Update(DateTime.UtcNow.AddDays(5));
            locator.Update(DateTime.UtcNow.AddDays(1), DateTime.UtcNow.AddDays(5));
            locator.UpdateAsync(DateTime.UtcNow.AddDays(5));
            locator.Delete();
            Assert.IsNull(_mediaContext.Locators.Where(c => c.Id == locator.Id).FirstOrDefault());

            ILocator sas = _mediaContext.Locators.CreateSasLocator(asset, policy);

            sas.Delete();

            sas = _mediaContext.Locators.CreateSasLocatorAsync(asset, policy).Result;
            sas.Delete();
            ILocator sasAsync = _mediaContext.Locators.CreateSasLocatorAsync(asset, policy, DateTime.UtcNow, "Name").Result;

            sasAsync.DeleteAsync().Wait();
        }
        public void OnlyOriginLocatorCanBeUpdated()
        {
            IAsset        asset   = _mediaContext.Assets.Create("Test", AssetCreationOptions.None);
            IAccessPolicy policy  = _mediaContext.AccessPolicies.Create("Test", TimeSpan.FromDays(1), AccessPermissions.Read);
            ILocator      locator = _mediaContext.Locators.CreateLocator(LocatorType.Sas, asset, policy);

            locator.Update(DateTime.UtcNow.AddDays(5));
        }
Beispiel #3
0
        public string GetLocatorUrl(IAsset asset, LocatorType locatorType, string fileName)
        {
            string   locatorUrl = string.Empty;
            ILocator locator    = asset.Locators.Where(l => l.Type == locatorType).LastOrDefault();

            if (locator != null)
            {
                if (locator.ExpirationDateTime <= DateTime.UtcNow)
                {
                    IAccessPolicy accessPolicy     = GetAccessPolicy(false);
                    DateTime      accessExpiration = DateTime.UtcNow.Add(accessPolicy.Duration);
                    locator.Update(accessExpiration);
                }
                locatorUrl = GetLocatorUrl(locator, fileName);
            }
            return(locatorUrl);
        }
Beispiel #4
0
        public string GetLocatorUrl(LocatorType locatorType, IAsset asset, string fileName, bool excludeProtocol)
        {
            ILocator locator = asset.Locators.Where(l => l.Type == locatorType).FirstOrDefault();

            if (locator == null)
            {
                locator = CreateLocator(locatorType, asset);
            }
            else if (locator.ExpirationDateTime <= DateTime.UtcNow)
            {
                string settingKey  = Constant.AppSettingKey.MediaLocatorAutoRenewal;
                string autoRenewal = AppSetting.GetValue(settingKey);
                if (string.Equals(autoRenewal, "true", StringComparison.OrdinalIgnoreCase))
                {
                    IAccessPolicy accessPolicy     = GetAccessPolicy(false);
                    DateTime      policyExpiration = DateTime.UtcNow.Add(accessPolicy.Duration);
                    locator.Update(policyExpiration);
                }
            }
            return(GetPrimaryUrl(locator, fileName, excludeProtocol));
        }
        private static ILocator CreateLocator(IAsset asset)
        {
            ILocator streamingLocator = _mediaContext
                                        .Locators
                                        .Where(l => l.AssetId == asset.Id)
                                        .AsEnumerable()
                                        .FirstOrDefault(l => l.Type == LocatorType.OnDemandOrigin);

            if (streamingLocator != null && streamingLocator.ExpirationDateTime <= DateTime.UtcNow + TimeSpan.FromHours(1))
            {
                streamingLocator.Delete();
                streamingLocator = null;
            }

            if (streamingLocator == null)
            {
                IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("readPolicy", TimeSpan.FromDays(7), AccessPermissions.Read);
                streamingLocator = _mediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset, accessPolicy);
            }

            streamingLocator.Update(DateTime.Now + TimeSpan.FromDays(7));

            return(streamingLocator);
        }