/// <summary> /// Copies resource from the source connection to another connection. /// </summary> /// <param name="sourceResourceIds">The array of source resource ids</param> /// <param name="targetResourceIds">The array of target resource ids to copy to. Each resource id in the source array will be copied to the corresponding resource id in the target array</param> /// <param name="overwrite">Indicates whether to overwrite </param> /// <param name="options">Re-base options</param> /// <param name="callback"></param> /// <returns></returns> public string[] CopyResources(string[] sourceResourceIds, string[] targetResourceIds, bool overwrite, RebaseOptions options, LengthyOperationProgressCallBack callback) { Check.ArgumentNotNull(sourceResourceIds, nameof(sourceResourceIds)); Check.ArgumentNotNull(targetResourceIds, nameof(targetResourceIds)); Check.ThatPreconditionIsMet(sourceResourceIds.Length == targetResourceIds.Length, $"{nameof(sourceResourceIds)}.Length == {nameof(targetResourceIds)}.Length"); var copiedItems = new List <string>(); var cb = callback; if (cb == null) { cb = new LengthyOperationProgressCallBack((s, e) => { //Do nothing }); } var targetCaps = _target.Capabilities; int copied = 0; int unit = 100 / sourceResourceIds.Length; int progress = 0; string message = string.Empty; for (int i = 0; i < sourceResourceIds.Length; i++) { var srcResId = sourceResourceIds[i]; var dstResId = targetResourceIds[i]; //Get the source resource object IResource res = _source.ResourceService.GetResource(srcResId); //Skip if target exists and overwrite is not specified if (!overwrite && _target.ResourceService.ResourceExists(dstResId)) { progress += unit; continue; } else { //Check if downgrading is required var maxVer = targetCaps.GetMaxSupportedResourceVersion(res.ResourceType); if (res.ResourceVersion > maxVer) { res = _converter.Convert(res, maxVer); cb(this, new LengthyOperationProgressArgs(string.Format(Strings.DowngradedResource, srcResId, maxVer), progress)); } //Now rebase if rebase options supplied if (options != null) { var rebaser = new ResourceRebaser(res); res = rebaser.Rebase(options.SourceFolder, options.TargetFolder); } //Save resource _target.ResourceService.SaveResourceAs(res, dstResId); //Copy resource data var resData = _source.ResourceService.EnumerateResourceData(res.ResourceID); foreach (var data in resData.ResourceData) { using (var stream = _source.ResourceService.GetResourceData(res.ResourceID, data.Name)) { if (!stream.CanSeek) { using (var ms = MemoryStreamPool.GetStream()) { Utility.CopyStream(stream, ms, false); ms.Position = 0L; _target.ResourceService.SetResourceData(dstResId, data.Name, data.Type, ms); } } else { stream.Position = 0L; _target.ResourceService.SetResourceData(dstResId, data.Name, data.Type, stream); } } } copied++; message = string.Format(Strings.CopiedResourceToTarget, srcResId, dstResId); } copiedItems.Add(srcResId); progress += unit; cb(this, new LengthyOperationProgressArgs(message, progress)); } return(copiedItems.ToArray()); }
/// <summary> /// Copies resource from the source connection to another connection. /// </summary> /// <param name="sourceResourceIds">The array of source resource ids</param> /// <param name="targetResourceIds">The array of target resource ids to copy to. Each resource id in the source array will be copied to the corresponding resource id in the target array</param> /// <param name="overwrite">Indicates whether to overwrite </param> /// <param name="options">Re-base options</param> /// <param name="callback"></param> /// <returns></returns> public string[] CopyResources(string[] sourceResourceIds, string[] targetResourceIds, bool overwrite, RebaseOptions options, LengthyOperationProgressCallBack callback) { Check.NotNull(sourceResourceIds, "sourceResourceIds"); //NOXLATE Check.NotNull(targetResourceIds, "targetResourceIds"); //NOXLATE Check.Precondition(sourceResourceIds.Length == targetResourceIds.Length, "resourceIds.Length == targetResourceIds.Length"); //NOXLATE var copiedItems = new List<string>(); var cb = callback; if (cb == null) { cb = new LengthyOperationProgressCallBack((s, e) => { //Do nothing }); } var targetCaps = _target.Capabilities; int copied = 0; int unit = 100 / sourceResourceIds.Length; int progress = 0; string message = string.Empty; for (int i = 0; i < sourceResourceIds.Length; i++) { var srcResId = sourceResourceIds[i]; var dstResId = targetResourceIds[i]; //Get the source resource object IResource res = _source.ResourceService.GetResource(srcResId); //Skip if target exists and overwrite is not specified if (!overwrite && _target.ResourceService.ResourceExists(dstResId)) { progress += unit; continue; } else { //Check if downgrading is required var maxVer = targetCaps.GetMaxSupportedResourceVersion(res.ResourceType); if (res.ResourceVersion > maxVer) { res = _converter.Convert(res, maxVer); cb(this, new LengthyOperationProgressArgs(string.Format(Strings.DowngradedResource, srcResId, maxVer), progress)); } //Now rebase if rebase options supplied if (options != null) { var rebaser = new ResourceRebaser(res); res = rebaser.Rebase(options.SourceFolder, options.TargetFolder); } //Save resource _target.ResourceService.SaveResourceAs(res, dstResId); //Copy resource data foreach (var data in res.EnumerateResourceData()) { using (var stream = res.GetResourceData(data.Name)) { if (!stream.CanSeek) { using (var ms = new MemoryStream()) { Utility.CopyStream(stream, ms, false); ms.Position = 0L; _target.ResourceService.SetResourceData(dstResId, data.Name, data.Type, ms); } } else { stream.Position = 0L; _target.ResourceService.SetResourceData(dstResId, data.Name, data.Type, stream); } } } copied++; message = string.Format(Strings.CopiedResourceToTarget, srcResId, dstResId); } copiedItems.Add(srcResId); progress += unit; cb(this, new LengthyOperationProgressArgs(message, progress)); } return copiedItems.ToArray(); }