コード例 #1
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.TryGetValue("src", out var src))
                {
                    currentPath = src.Value <string>();
                }
            }
            catch (Exception ex)
            {
                // For some reason the value is invalid so continue as if there was no value there
                _logger.LogWarning(ex, "Could not parse current db value to a JObject.");
            }

            if (string.IsNullOrWhiteSpace(currentPath) == false)
            {
                currentPath = _mediaFileManager.FileSystem.GetRelativePath(currentPath);
            }

            // Get the new JSON and file path
            var editorFile = string.Empty;
            var editorJson = (JObject?)editorValue.Value;

            if (editorJson is not null)
            {
                // Populate current file
                if (editorJson["src"] != null)
                {
                    editorFile = editorJson["src"]?.Value <string>();
                }

                // Clean up redundant/default data
                ImageCropperValue.Prune(editorJson);
            }
            else
            {
                editorJson = null;
            }

            // 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)
                {
                    _mediaFileManager.FileSystem.DeleteFile(currentPath);
                    return(null); // clear
                }

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

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

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

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

            // update json and return
            if (editorJson == null)
            {
                return(null);
            }
            editorJson["src"] = filepath == null ? string.Empty : _mediaFileManager.FileSystem.GetUrl(filepath);
            return(editorJson.ToString(Formatting.None));
        }
コード例 #2
0
 /// <summary>
 /// Removes redundant crop data/default focal point.
 /// </summary>
 /// <param name="value">The media with crops DTO.</param>
 /// <remarks>
 /// Because the DTO uses the same JSON keys as the image cropper value for crops and focal point, we can re-use the prune method.
 /// </remarks>
 public static void Prune(JObject?value) => ImageCropperValue.Prune(value);