/// <summary>
        /// Look through all property values stored against the property and resolve any file paths stored
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        private IEnumerable <string> GetFilePathsFromPropertyValues(Property prop)
        {
            var propVals = prop.Values;

            foreach (var propertyValue in propVals)
            {
                //check if the published value contains data and return it
                var propVal = propertyValue.PublishedValue;
                if (propVal != null && propVal is string str1 && !str1.IsNullOrWhiteSpace())
                {
                    yield return(_mediaFileSystem.GetRelativePath(str1));
                }

                //check if the edited value contains data and return it
                propVal = propertyValue.EditedValue;
                if (propVal != null && propVal is string str2 && !str2.IsNullOrWhiteSpace())
                {
                    yield return(_mediaFileSystem.GetRelativePath(str2));
                }
            }
        }
        /// <summary>
        /// Look through all property values stored against the property and resolve any file paths stored
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        private IEnumerable <string> GetFilePathsFromPropertyValues(Property prop)
        {
            //parses out the src from a json string

            foreach (var propertyValue in prop.Values)
            {
                //check if the published value contains data and return it
                var src = GetFileSrcFromPropertyValue(propertyValue.PublishedValue, out var _);
                if (src != null)
                {
                    yield return(_mediaFileSystem.GetRelativePath(src));
                }

                //check if the edited value contains data and return it
                src = GetFileSrcFromPropertyValue(propertyValue.EditedValue, out var _);
                if (src != null)
                {
                    yield return(_mediaFileSystem.GetRelativePath(src));
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Converts the value received from the editor into the value can be stored in the database.
        /// </summary>
        /// <param name="editorValue">The value received from the editor.</param>
        /// <param name="currentValue">The current value of the property</param>
        /// <returns>The converted value.</returns>
        /// <remarks>
        /// <para>The <paramref name="currentValue"/> is used to re-use the folder, if possible.</para>
        /// <para>editorValue.Value is used to figure out editorFile and, if it has been cleared, remove the old file - but
        /// it is editorValue.AdditionalData["files"] that is used to determine the actual file that has been uploaded.</para>
        /// </remarks>
        public override object FromEditor(ContentPropertyData editorValue, object currentValue)
        {
            // get the current path
            var currentPath = string.Empty;

            try
            {
                var svalue      = currentValue as string;
                var currentJson = string.IsNullOrWhiteSpace(svalue) ? null : JObject.Parse(svalue);
                if (currentJson != null && currentJson["src"] != null)
                {
                    currentPath = currentJson["src"].Value <string>();
                }
            }
            catch (Exception ex)
            {
                // for some reason the value is invalid so continue as if there was no value there
                _logger.Warn <ImageCropperPropertyValueEditor>(ex, "Could not parse current db value to a JObject.");
            }
            if (string.IsNullOrWhiteSpace(currentPath) == false)
            {
                currentPath = _mediaFileSystem.GetRelativePath(currentPath);
            }

            // get the new json and path
            JObject editorJson = null;
            var     editorFile = string.Empty;

            if (editorValue.Value != null)
            {
                editorJson = editorValue.Value as JObject;
                if (editorJson != null && editorJson["src"] != null)
                {
                    editorFile = editorJson["src"].Value <string>();
                }
            }

            // ensure we have the required guids
            var cuid = editorValue.ContentKey;

            if (cuid == Guid.Empty)
            {
                throw new Exception("Invalid content key.");
            }
            var puid = editorValue.PropertyTypeKey;

            if (puid == Guid.Empty)
            {
                throw new Exception("Invalid property type key.");
            }

            // editorFile is empty whenever a new file is being uploaded
            // or when the file is cleared (in which case editorJson is null)
            // else editorFile contains the unchanged value

            var uploads = editorValue.Files;

            if (uploads == null)
            {
                throw new Exception("Invalid files.");
            }
            var file = uploads.Length > 0 ? uploads[0] : null;

            if (file == null) // not uploading a file
            {
                // if editorFile is empty then either there was nothing to begin with,
                // or it has been cleared and we need to remove the file - else the
                // value is unchanged.
                if (string.IsNullOrWhiteSpace(editorFile) && string.IsNullOrWhiteSpace(currentPath) == false)
                {
                    _mediaFileSystem.DeleteFile(currentPath);
                    return(null); // clear
                }

                return(editorJson?.ToString()); // unchanged
            }

            // process the file
            var filepath = editorJson == null ? null : ProcessFile(editorValue, file, currentPath, cuid, puid);

            // remove all temp files
            foreach (var f in uploads)
            {
                File.Delete(f.TempFilePath);
            }

            // remove current file if replaced
            if (currentPath != filepath && string.IsNullOrWhiteSpace(currentPath) == false)
            {
                _mediaFileSystem.DeleteFile(currentPath);
            }

            // update json and return
            if (editorJson == null)
            {
                return(null);
            }
            editorJson["src"] = filepath == null ? string.Empty : _mediaFileSystem.GetUrl(filepath);
            return(editorJson.ToString());
        }
        /// <summary>
        /// Converts the value received from the editor into the value can be stored in the database.
        /// </summary>
        /// <param name="editorValue">The value received from the editor.</param>
        /// <param name="currentValue">The current value of the property</param>
        /// <returns>The converted value.</returns>
        /// <remarks>
        /// <para>The <paramref name="currentValue"/> is used to re-use the folder, if possible.</para>
        /// <para>The <paramref name="editorValue"/> is value passed in from the editor. We normally don't care what
        /// the editorValue.Value is set to because we are more interested in the files collection associated with it,
        /// however we do care about the value if we are clearing files. By default the editorValue.Value will just
        /// be set to the name of the file - but again, we just ignore this and deal with the file collection in
        /// editorValue.AdditionalData.ContainsKey("files")</para>
        /// <para>We only process ONE file. We understand that the current value may contain more than one file,
        /// and that more than one file may be uploaded, so we take care of them all, but we only store ONE file.
        /// Other places (FileUploadPropertyEditor...) do NOT deal with multiple files, and our logic for reusing
        /// folders would NOT work, etc.</para>
        /// </remarks>
        public override object FromEditor(ContentPropertyData editorValue, object currentValue)
        {
            var currentPath = currentValue as string;

            if (!currentPath.IsNullOrWhiteSpace())
            {
                currentPath = _mediaFileSystem.GetRelativePath(currentPath);
            }

            string editorFile = null;

            if (editorValue.Value != null)
            {
                editorFile = editorValue.Value as string;
            }

            // ensure we have the required guids
            var cuid = editorValue.ContentKey;

            if (cuid == Guid.Empty)
            {
                throw new Exception("Invalid content key.");
            }
            var puid = editorValue.PropertyTypeKey;

            if (puid == Guid.Empty)
            {
                throw new Exception("Invalid property type key.");
            }

            var uploads = editorValue.Files;

            if (uploads == null)
            {
                throw new Exception("Invalid files.");
            }
            var file = uploads.Length > 0 ? uploads[0] : null;

            if (file == null) // not uploading a file
            {
                // if editorFile is empty then either there was nothing to begin with,
                // or it has been cleared and we need to remove the file - else the
                // value is unchanged.
                if (string.IsNullOrWhiteSpace(editorFile) && string.IsNullOrWhiteSpace(currentPath) == false)
                {
                    _mediaFileSystem.DeleteFile(currentPath);
                    return(null); // clear
                }

                return(currentValue); // unchanged
            }

            // process the file
            var filepath = editorFile == null ? null : ProcessFile(editorValue, file, currentPath, cuid, puid);

            // remove all temp files
            foreach (var f in uploads)
            {
                File.Delete(f.TempFilePath);
            }

            // remove current file if replaced
            if (currentPath != filepath && string.IsNullOrWhiteSpace(currentPath) == false)
            {
                _mediaFileSystem.DeleteFile(currentPath);
            }

            // update json and return
            if (editorFile == null)
            {
                return(null);
            }
            return(filepath == null ? string.Empty : _mediaFileSystem.GetUrl(filepath));
        }