object IMetaWeblog.NewMediaObject(string blogid, string username, string password, MetaWeblogMediaObject media) { ValidateUser(username, password); // Save File using (var ms = new MemoryStream(media.Bits)) { var fileUrl = "articulate/" + media.Name.ToSafeFileName(); _mediaFileSystem.AddFile(fileUrl, ms); var absUrl = _mediaFileSystem.GetUrl(fileUrl); return(new { url = absUrl }); } }
/// <summary> /// After a content has been copied, also copy uploaded files. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="args">The event arguments.</param> public void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs <IContent> args) { // get the image cropper field properties var properties = args.Original.Properties.Where(IsCropperField); // copy files var isUpdated = false; foreach (var property in properties) { //copy each of the property values (variants, segments) to the destination by using the edited value foreach (var propertyValue in property.Values) { var propVal = property.GetValue(propertyValue.Culture, propertyValue.Segment); var src = GetFileSrcFromPropertyValue(propVal, out var jo); if (src == null) { continue; } var sourcePath = _mediaFileSystem.GetRelativePath(src); var copyPath = _mediaFileSystem.CopyFile(args.Copy, property.PropertyType, sourcePath); jo["src"] = _mediaFileSystem.GetUrl(copyPath); args.Copy.SetValue(property.Alias, jo.ToString(), propertyValue.Culture, propertyValue.Segment); isUpdated = true; } } // if updated, re-save the copy with the updated value if (isUpdated) { sender.Save(args.Copy); } }
/// <summary> /// After a content has been copied, also copy uploaded files. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="args">The event arguments.</param> internal void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs <IContent> args) { // get the upload field properties with a value var properties = args.Original.Properties.Where(IsUploadField); // copy files var isUpdated = false; foreach (var property in properties) { //copy each of the property values (variants, segments) to the destination foreach (var propertyValue in property.Values) { var propVal = property.GetValue(propertyValue.Culture, propertyValue.Segment); if (propVal == null || !(propVal is string str) || str.IsNullOrWhiteSpace()) { continue; } var sourcePath = _mediaFileSystem.GetRelativePath(str); var copyPath = _mediaFileSystem.CopyFile(args.Copy, property.PropertyType, sourcePath); args.Copy.SetValue(property.Alias, _mediaFileSystem.GetUrl(copyPath), propertyValue.Culture, propertyValue.Segment); isUpdated = true; } } // if updated, re-save the copy with the updated value if (isUpdated) { sender.Save(args.Copy); } }
/// <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)); }