public void DeleteMediaItem(ResourcePath path)
 {
     try
     {
         _contentDirectory.DeleteMediaItemOrPath(_localSystemId, path, true);
     }
     catch (Exception)
     {
         throw new DisconnectedException();
     }
 }
        public override bool Process(MediaItem mediaItem, out ContentDirectoryMessaging.MediaItemChangeType changeType)
        {
            changeType = ContentDirectoryMessaging.MediaItemChangeType.None;

            var rl = mediaItem.GetResourceLocator();

            using (var ra = rl.CreateAccessor())
            {
                var rad = ra as IResourceDeletor;
                if (rad == null)
                {
                    return(false);
                }

                // First try to delete the file from storage.
                if (rad.Delete())
                {
                    changeType = ContentDirectoryMessaging.MediaItemChangeType.Deleted;

                    // If the MediaItem was loaded from ML, remove it there as well.
                    if (IsManagedByMediaLibrary(mediaItem))
                    {
                        IContentDirectory cd = ServiceRegistration.Get <IServerConnectionManager>().ContentDirectory;
                        if (cd == null)
                        {
                            return(true);
                        }

                        cd.DeleteMediaItemOrPath(rl.NativeSystemId, rl.NativeResourcePath, true);
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #3
0
        public override bool Process(MediaItem mediaItem, out ContentDirectoryMessaging.MediaItemChangeType changeType)
        {
            IContentDirectory cd = ServiceRegistration.Get <IServerConnectionManager>().ContentDirectory;
            bool removeFromML    = IsManagedByMediaLibrary(mediaItem) && cd != null;

            changeType = ContentDirectoryMessaging.MediaItemChangeType.None;

            // Support multi-resource media items and secondary resources
            IList <MultipleMediaItemAspect> providerAspects;

            if (!MediaItemAspect.TryGetAspects(mediaItem.Aspects, ProviderResourceAspect.Metadata, out providerAspects))
            {
                return(false);
            }

            foreach (MultipleMediaItemAspect providerAspect in providerAspects)
            {
                string systemId             = (string)providerAspect[ProviderResourceAspect.ATTR_SYSTEM_ID];
                string resourceAccessorPath = (string)providerAspect[ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH];
                var    rl = new ResourceLocator(systemId, ResourcePath.Deserialize(resourceAccessorPath));
                using (var ra = rl.CreateAccessor())
                {
                    var rad = ra as IResourceDeletor;
                    if (rad == null)
                    {
                        return(false);
                    }

                    // First try to delete the file from storage.
                    if (!rad.Delete())
                    {
                        return(false);
                    }

                    changeType = ContentDirectoryMessaging.MediaItemChangeType.Deleted;

                    // If the MediaItem was loaded from ML, remove it there as well.
                    if (removeFromML)
                    {
                        cd.DeleteMediaItemOrPath(rl.NativeSystemId, rl.NativeResourcePath, true);
                    }
                }
            }

            // Check for special cases here:
            // 1) Recordings have an additional .xml attached
            // 2) Deleting files could lead to empty folders that should be also removed
            foreach (DeleteRule rule in _defaultRules.Where(r => r.IsEnabled))
            {
                if (mediaItem.Aspects.ContainsKey(rule.HasAspectGuid))
                {
                    var tsPath = mediaItem.GetResourceLocator().NativeResourcePath.ToString();
                    foreach (string otherExtension in rule.DeleteOtherExtensions)
                    {
                        string            otherFilePath = ProviderPathHelper.ChangeExtension(tsPath, otherExtension);
                        IResourceAccessor ra;
                        if (!ResourcePath.Deserialize(otherFilePath).TryCreateLocalResourceAccessor(out ra))
                        {
                            continue;
                        }

                        // Delete other file. We do not check here for existance of file, the Delete needs to handle this.
                        using (ra)
                        {
                            var rad = ra as IResourceDeletor;
                            rad?.Delete();
                        }
                    }

                    if (rule.DeleteEmptyFolders)
                    {
                        var folderPath = ProviderPathHelper.GetDirectoryName(tsPath);
                        IResourceAccessor ra;
                        if (!ResourcePath.Deserialize(folderPath).TryCreateLocalResourceAccessor(out ra))
                        {
                            continue;
                        }

                        // Delete folder if empty
                        using (ra)
                        {
                            IFileSystemResourceAccessor fsra = ra as IFileSystemResourceAccessor;
                            if (fsra != null)
                            {
                                var isEmpty = fsra.GetFiles().Count == 0 && fsra.GetChildDirectories().Count == 0;
                                if (isEmpty)
                                {
                                    var rad = ra as IResourceDeletor;
                                    rad?.Delete();
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }