示例#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>
        /// Stores a file.
        /// </summary>
        /// <param name="content"><see cref="IContentBase"/>A content item.</param>
        /// <param name="propertyTypeAlias">The property alias.</param>
        /// <param name="filename">The name of the file.</param>
        /// <param name="filestream">A stream containing the file data.</param>
        /// <param name="filepath">The original file path, if any.</param>
        /// <returns>The path to the file, relative to the media filesystem.</returns>
        /// <remarks>
        /// <para>Does NOT set the property value, so one should probably store the file and then do
        /// something alike: property.Value = MediaHelper.FileSystem.GetUrl(filepath).</para>
        /// <para>The original file path is used, in the old media file path scheme, to try and reuse
        /// the "folder number" that was assigned to the previous file referenced by the property,
        /// if any.</para>
        /// </remarks>
        public static string StoreFile(this IContentBase content, MediaFileManager mediaFileManager, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, string propertyTypeAlias, string filename, Stream filestream, string filepath)
        {
            var contentType  = contentTypeBaseServiceProvider.GetContentTypeOf(content);
            var propertyType = contentType
                               .CompositionPropertyTypes.FirstOrDefault(x => x.Alias.InvariantEquals(propertyTypeAlias));

            if (propertyType == null)
            {
                throw new ArgumentException("Invalid property type alias " + propertyTypeAlias + ".");
            }
            return(mediaFileManager.StoreFile(content, propertyType, filename, filestream, filepath));
        }