예제 #1
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);
        }
예제 #2
0
        /// <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);
        }