Exemple #1
0
        /// <summary>
        /// Sets the posted file value of a property.
        /// </summary>
        public static void SetValue(
            this IContentBase content,
            MediaFileManager mediaFileManager,
            MediaUrlGeneratorCollection mediaUrlGenerators,
            IShortStringHelper shortStringHelper,
            IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
            string propertyTypeAlias,
            string filename,
            Stream filestream,
            string culture = null,
            string segment = null)
        {
            if (filename == null || filestream == null)
            {
                return;
            }

            filename = shortStringHelper.CleanStringForSafeFileName(filename);
            if (string.IsNullOrWhiteSpace(filename))
            {
                return;
            }
            filename = filename.ToLower();

            SetUploadFile(content, mediaFileManager, mediaUrlGenerators, contentTypeBaseServiceProvider, propertyTypeAlias, filename, filestream, culture, segment);
        }
        /// <summary>
        /// Returns a stream (file) for a content item or null if there is no file.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="mediaFilePath">The file path if a file was found</param>
        /// <param name="propertyTypeAlias"></param>
        /// <param name="variationContextAccessor"></param>
        /// <returns></returns>
        public Stream GetFile(
            IContentBase content,
            out string mediaFilePath,
            string propertyTypeAlias = Constants.Conventions.Media.File,
            string culture           = null,
            string segment           = null)
        {
            // TODO: If collections were lazy we could just inject them
            if (_mediaUrlGenerators == null)
            {
                _mediaUrlGenerators = _serviceProvider.GetRequiredService <MediaUrlGeneratorCollection>();
            }

            if (!content.TryGetMediaPath(propertyTypeAlias, _mediaUrlGenerators, out mediaFilePath, culture, segment))
            {
                return(null);
            }

            Stream stream = FileSystem.OpenFile(mediaFilePath);

            if (stream != null)
            {
                return(stream);
            }

            mediaFilePath = null;
            return(null);
        }
 public MediaRepository(
     IScopeAccessor scopeAccessor,
     AppCaches cache,
     ILogger <MediaRepository> logger,
     ILoggerFactory loggerFactory,
     IMediaTypeRepository mediaTypeRepository,
     ITagRepository tagRepository,
     ILanguageRepository languageRepository,
     IRelationRepository relationRepository,
     IRelationTypeRepository relationTypeRepository,
     PropertyEditorCollection propertyEditorCollection,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     DataValueReferenceFactoryCollection dataValueReferenceFactories,
     IDataTypeService dataTypeService,
     IJsonSerializer serializer,
     IEventAggregator eventAggregator)
     : base(scopeAccessor, cache, logger, languageRepository, relationRepository, relationTypeRepository, propertyEditorCollection, dataValueReferenceFactories, dataTypeService, eventAggregator)
 {
     _cache = cache;
     _mediaTypeRepository       = mediaTypeRepository ?? throw new ArgumentNullException(nameof(mediaTypeRepository));
     _tagRepository             = tagRepository ?? throw new ArgumentNullException(nameof(tagRepository));
     _mediaUrlGenerators        = mediaUrlGenerators;
     _serializer                = serializer;
     _mediaByGuidReadRepository = new MediaByGuidReadRepository(this, scopeAccessor, cache, loggerFactory.CreateLogger <MediaByGuidReadRepository>());
 }
Exemple #4
0
        private static void SetUploadFile(
            this IContentBase content,
            MediaFileManager mediaFileManager,
            MediaUrlGeneratorCollection mediaUrlGenerators,
            IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
            string propertyTypeAlias,
            string filename,
            Stream filestream,
            string culture = null,
            string segment = null)
        {
            var property = GetProperty(content, contentTypeBaseServiceProvider, propertyTypeAlias);

            // Fixes https://github.com/umbraco/Umbraco-CMS/issues/3937 - Assigning a new file to an
            // existing IMedia with extension SetValue causes exception 'Illegal characters in path'
            string oldpath = null;

            if (content.TryGetMediaPath(property.Alias, mediaUrlGenerators, out string mediaFilePath, culture, segment))
            {
                oldpath = mediaFileManager.FileSystem.GetRelativePath(mediaFilePath);
            }

            var filepath = mediaFileManager.StoreFile(content, property.PropertyType, filename, filestream, oldpath);

            // NOTE: Here we are just setting the value to a string which means that any file based editor
            // will need to handle the raw string value and save it to it's correct (i.e. JSON)
            // format. I'm unsure how this works today with image cropper but it does (maybe events?)
            property.SetValue(mediaFileManager.FileSystem.GetUrl(filepath), culture, segment);
        }
        /// <summary>
        /// Gets the URL of a media item.
        /// </summary>
        public static string GetUrl(this IMedia media, string propertyAlias, MediaUrlGeneratorCollection mediaUrlGenerators)
        {
            if (media.TryGetMediaPath(propertyAlias, mediaUrlGenerators, out var mediaPath))
            {
                return(mediaPath);
            }

            return(string.Empty);
        }
 public MigrateToPackageData(
     IPackagingService packagingService,
     IMediaService mediaService,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IShortStringHelper shortStringHelper,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     IMigrationContext context)
     : base(packagingService, mediaService, mediaFileManager, mediaUrlGenerators, shortStringHelper, contentTypeBaseServiceProvider, context)
 {
 }
Exemple #7
0
 public SetupuSync(
     IPackagingService packagingService,
     IMediaService mediaService,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IShortStringHelper shortStringHelper,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     IMigrationContext context,
     IOptions <PackageMigrationSettings> packageMigrationsSettings)
     : base(packagingService, mediaService, mediaFileManager, mediaUrlGenerators, shortStringHelper, contentTypeBaseServiceProvider, context, packageMigrationsSettings)
 {
 }
Exemple #8
0
        public MediaMapDefinition(ICultureDictionary cultureDictionary, CommonMapper commonMapper, CommonTreeNodeMapper commonTreeNodeMapper, IMediaService mediaService, IMediaTypeService mediaTypeService,
                                  ILocalizedTextService localizedTextService, MediaUrlGeneratorCollection mediaUrlGenerators, IOptions <ContentSettings> contentSettings, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider)
        {
            _commonMapper         = commonMapper;
            _commonTreeNodeMapper = commonTreeNodeMapper;
            _mediaService         = mediaService;
            _mediaTypeService     = mediaTypeService;
            _mediaUrlGenerators   = mediaUrlGenerators;
            _contentSettings      = contentSettings.Value ?? throw new ArgumentNullException(nameof(contentSettings));

            _tabsAndPropertiesMapper = new TabsAndPropertiesMapper <IMedia>(cultureDictionary, localizedTextService, contentTypeBaseServiceProvider);
        }
        /// <summary>
        /// Builds a dto from an IMedia item.
        /// </summary>
        public static MediaDto BuildDto(MediaUrlGeneratorCollection mediaUrlGenerators, IMedia entity)
        {
            var contentDto = BuildContentDto(entity, Cms.Core.Constants.ObjectTypes.Media);

            var dto = new MediaDto
            {
                NodeId          = entity.Id,
                ContentDto      = contentDto,
                MediaVersionDto = BuildMediaVersionDto(mediaUrlGenerators, entity, contentDto)
            };

            return(dto);
        }
Exemple #10
0
 public MediaValueSetBuilder(
     PropertyEditorCollection propertyEditors,
     UrlSegmentProviderCollection urlSegmentProviders,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IUserService userService,
     IShortStringHelper shortStringHelper,
     IOptions <ContentSettings> contentSettings)
     : base(propertyEditors, false)
 {
     _urlSegmentProviders = urlSegmentProviders;
     _mediaUrlGenerators  = mediaUrlGenerators;
     _userService         = userService;
     _shortStringHelper   = shortStringHelper;
     _contentSettings     = contentSettings.Value;
 }
Exemple #11
0
 public IndexInitializer(
     IShortStringHelper shortStringHelper,
     PropertyEditorCollection propertyEditors,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IScopeProvider scopeProvider,
     ILoggerFactory loggerFactory,
     IOptions <ContentSettings> contentSettings)
 {
     _shortStringHelper  = shortStringHelper;
     _propertyEditors    = propertyEditors;
     _mediaUrlGenerators = mediaUrlGenerators;
     _scopeProvider      = scopeProvider;
     _loggerFactory      = loggerFactory;
     _contentSettings    = contentSettings;
 }
 public ImportPackageBuilderExpression(
     IPackagingService packagingService,
     IMediaService mediaService,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IShortStringHelper shortStringHelper,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     IMigrationContext context) : base(context)
 {
     _packagingService               = packagingService;
     _mediaService                   = mediaService;
     _mediaFileManager               = mediaFileManager;
     _mediaUrlGenerators             = mediaUrlGenerators;
     _shortStringHelper              = shortStringHelper;
     _contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
 }
 public MediaController(
     ICultureDictionary cultureDictionary,
     ILoggerFactory loggerFactory,
     IShortStringHelper shortStringHelper,
     IEventMessagesFactory eventMessages,
     ILocalizedTextService localizedTextService,
     IOptions <ContentSettings> contentSettings,
     IMediaTypeService mediaTypeService,
     IMediaService mediaService,
     IEntityService entityService,
     IBackOfficeSecurityAccessor backofficeSecurityAccessor,
     IUmbracoMapper umbracoMapper,
     IDataTypeService dataTypeService,
     ISqlContext sqlContext,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     IRelationService relationService,
     PropertyEditorCollection propertyEditors,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IHostingEnvironment hostingEnvironment,
     IImageUrlGenerator imageUrlGenerator,
     IJsonSerializer serializer,
     IAuthorizationService authorizationService,
     AppCaches appCaches)
     : base(cultureDictionary, loggerFactory, shortStringHelper, eventMessages, localizedTextService, serializer)
 {
     _shortStringHelper          = shortStringHelper;
     _contentSettings            = contentSettings.Value;
     _mediaTypeService           = mediaTypeService;
     _mediaService               = mediaService;
     _entityService              = entityService;
     _backofficeSecurityAccessor = backofficeSecurityAccessor;
     _umbracoMapper              = umbracoMapper;
     _dataTypeService            = dataTypeService;
     _localizedTextService       = localizedTextService;
     _sqlContext = sqlContext;
     _contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
     _relationService      = relationService;
     _propertyEditors      = propertyEditors;
     _mediaFileManager     = mediaFileManager;
     _mediaUrlGenerators   = mediaUrlGenerators;
     _hostingEnvironment   = hostingEnvironment;
     _logger               = loggerFactory.CreateLogger <MediaController>();
     _imageUrlGenerator    = imageUrlGenerator;
     _authorizationService = authorizationService;
     _appCaches            = appCaches;
 }
        public override void SetUp()
        {
            base.SetUp();

            var loggerFactory    = NullLoggerFactory.Instance;
            var mediaFileManager = new MediaFileManager(Mock.Of <IFileSystem>(), Mock.Of <IMediaPathScheme>(),
                                                        loggerFactory.CreateLogger <MediaFileManager>(), Mock.Of <IShortStringHelper>());
            var contentSettings = new ContentSettings();
            var dataTypeService = Mock.Of <IDataTypeService>();
            var propertyEditors = new MediaUrlGeneratorCollection(new IMediaUrlGenerator[]
            {
                new FileUploadPropertyEditor(DataValueEditorFactory, mediaFileManager, Microsoft.Extensions.Options.Options.Create(contentSettings), dataTypeService, LocalizationService, LocalizedTextService, UploadAutoFillProperties, ContentService),
                new ImageCropperPropertyEditor(DataValueEditorFactory, loggerFactory, mediaFileManager, Microsoft.Extensions.Options.Options.Create(contentSettings), dataTypeService, IOHelper, UploadAutoFillProperties, ContentService),
            });

            _mediaUrlProvider = new DefaultMediaUrlProvider(propertyEditors, UriUtility);
        }
 public ImportPackageBuilder(
     IPackagingService packagingService,
     IMediaService mediaService,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IShortStringHelper shortStringHelper,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     IMigrationContext context)
     : base(new ImportPackageBuilderExpression(
                packagingService,
                mediaService,
                mediaFileManager,
                mediaUrlGenerators,
                shortStringHelper,
                contentTypeBaseServiceProvider,
                context))
 {
 }
 public PackageMigrationBase(
     IPackagingService packagingService,
     IMediaService mediaService,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IShortStringHelper shortStringHelper,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     IMigrationContext context,
     IOptions <PackageMigrationSettings> packageMigrationsSettings)
     : base(context)
 {
     _packagingService               = packagingService;
     _mediaService                   = mediaService;
     _mediaFileManager               = mediaFileManager;
     _mediaUrlGenerators             = mediaUrlGenerators;
     _shortStringHelper              = shortStringHelper;
     _contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
     _packageMigrationsSettings      = packageMigrationsSettings;
 }
 public PackageMigrationBase(
     IPackagingService packagingService,
     IMediaService mediaService,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IShortStringHelper shortStringHelper,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     IMigrationContext context)
     : this(
         packagingService,
         mediaService,
         mediaFileManager,
         mediaUrlGenerators,
         shortStringHelper,
         contentTypeBaseServiceProvider,
         context,
         StaticServiceProvider.Instance.GetRequiredService <IOptions <PackageMigrationSettings> >())
 {
 }
Exemple #18
0
        private MediaRepository CreateRepository(IScopeProvider provider, out MediaTypeRepository mediaTypeRepository, AppCaches appCaches = null)
        {
            appCaches ??= AppCaches.NoCache;
            var scopeAccessor      = (IScopeAccessor)provider;
            var globalSettings     = new GlobalSettings();
            var commonRepository   = new ContentTypeCommonRepository(scopeAccessor, TemplateRepository, appCaches, ShortStringHelper);
            var languageRepository = new LanguageRepository(scopeAccessor, appCaches, LoggerFactory.CreateLogger <LanguageRepository>());

            mediaTypeRepository = new MediaTypeRepository(scopeAccessor, appCaches, LoggerFactory.CreateLogger <MediaTypeRepository>(), commonRepository, languageRepository, ShortStringHelper);
            var tagRepository          = new TagRepository(scopeAccessor, appCaches, LoggerFactory.CreateLogger <TagRepository>());
            var relationTypeRepository = new RelationTypeRepository(scopeAccessor, AppCaches.Disabled, LoggerFactory.CreateLogger <RelationTypeRepository>());
            var entityRepository       = new EntityRepository(scopeAccessor, AppCaches.Disabled);
            var relationRepository     = new RelationRepository(scopeAccessor, LoggerFactory.CreateLogger <RelationRepository>(), relationTypeRepository, entityRepository);
            var propertyEditors        = new PropertyEditorCollection(new DataEditorCollection(() => Enumerable.Empty <IDataEditor>()));
            var mediaUrlGenerators     = new MediaUrlGeneratorCollection(() => Enumerable.Empty <IMediaUrlGenerator>());
            var dataValueReferences    = new DataValueReferenceFactoryCollection(() => Enumerable.Empty <IDataValueReferenceFactory>());
            var repository             = new MediaRepository(scopeAccessor, appCaches, LoggerFactory.CreateLogger <MediaRepository>(), LoggerFactory, mediaTypeRepository, tagRepository, Mock.Of <ILanguageRepository>(), relationRepository, relationTypeRepository, propertyEditors, mediaUrlGenerators, dataValueReferences, DataTypeService, JsonSerializer, Mock.Of <IEventAggregator>());

            return(repository);
        }
Exemple #19
0
 public RichTextEditorPastedImages(
     IUmbracoContextAccessor umbracoContextAccessor,
     ILogger <RichTextEditorPastedImages> logger,
     IHostingEnvironment hostingEnvironment,
     IMediaService mediaService,
     IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
     MediaFileManager mediaFileManager,
     MediaUrlGeneratorCollection mediaUrlGenerators,
     IShortStringHelper shortStringHelper,
     IPublishedUrlProvider publishedUrlProvider)
 {
     _umbracoContextAccessor =
         umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor));
     _logger                         = logger ?? throw new ArgumentNullException(nameof(logger));
     _hostingEnvironment             = hostingEnvironment;
     _mediaService                   = mediaService ?? throw new ArgumentNullException(nameof(mediaService));
     _contentTypeBaseServiceProvider = contentTypeBaseServiceProvider ??
                                       throw new ArgumentNullException(nameof(contentTypeBaseServiceProvider));
     _mediaFileManager     = mediaFileManager;
     _mediaUrlGenerators   = mediaUrlGenerators;
     _shortStringHelper    = shortStringHelper;
     _publishedUrlProvider = publishedUrlProvider;
 }
        private static MediaVersionDto BuildMediaVersionDto(MediaUrlGeneratorCollection mediaUrlGenerators, IMedia entity, ContentDto contentDto)
        {
            // try to get a path from the string being stored for media
            // TODO: only considering umbracoFile

            string path = null;

            if (entity.Properties.TryGetValue(Cms.Core.Constants.Conventions.Media.File, out var property) &&
                mediaUrlGenerators.TryGetMediaPath(property.PropertyType.PropertyEditorAlias, property.GetValue(), out var mediaPath))
            {
                path = mediaPath;
            }

            var dto = new MediaVersionDto
            {
                Id   = entity.VersionId,
                Path = path,

                ContentVersionDto = BuildContentVersionDto(entity, contentDto)
            };

            return(dto);
        }
Exemple #21
0
        /// <summary>
        /// Returns the path to a media item stored in a property if the property editor is <see cref="IMediaUrlGenerator"/>
        /// </summary>
        /// <param name="content"></param>
        /// <param name="propertyTypeAlias"></param>
        /// <param name="mediaUrlGenerators"></param>
        /// <param name="mediaFilePath"></param>
        /// <param name="culture"></param>
        /// <param name="segment"></param>
        /// <returns>True if the file path can be resolved and the property is <see cref="IMediaUrlGenerator"/></returns>
        public static bool TryGetMediaPath(
            this IContentBase content,
            string propertyTypeAlias,
            MediaUrlGeneratorCollection mediaUrlGenerators,
            out string mediaFilePath,
            string culture = null,
            string segment = null)
        {
            if (!content.Properties.TryGetValue(propertyTypeAlias, out IProperty property))
            {
                mediaFilePath = null;
                return(false);
            }

            if (!mediaUrlGenerators.TryGetMediaPath(
                    property.PropertyType.PropertyEditorAlias,
                    property.GetValue(culture, segment),
                    out mediaFilePath))
            {
                return(false);
            }

            return(true);
        }
 /// <summary>
 /// Gets the URLs of a media item.
 /// </summary>
 public static string[] GetUrls(this IMedia media, ContentSettings contentSettings, MediaUrlGeneratorCollection mediaUrlGenerators)
 => contentSettings.Imaging.AutoFillImageProperties
 .Select(field => media.GetUrl(field.Alias, mediaUrlGenerators))
 .Where(link => string.IsNullOrWhiteSpace(link) == false)
 .ToArray();
Exemple #23
0
 public DefaultMediaUrlProvider(MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
 {
     _mediaPathGenerators = mediaPathGenerators ?? throw new ArgumentNullException(nameof(mediaPathGenerators));
     _uriUtility          = uriUtility;
 }