// 在服务器端创建对象 // parameters: // strStyle 风格。当创建目录的时候,为"createdir",否则为空 // return: // -1 错误 // 1 以及存在同名对象 // 0 正常返回 int NewServerSideObject( RmsChannel channel, string strPath, string strStyle, Stream stream, byte[] baTimeStamp, out string strError) { strError = ""; byte[] baOutputTimestamp = null; string strOutputPath = ""; string strRange = ""; if (stream != null && stream.Length != 0) { Debug.Assert(stream.Length != 0, "test"); strRange = "0-" + Convert.ToString(stream.Length - 1); } long lRet = channel.DoSaveResObject(strPath, stream, (stream != null && stream.Length != 0) ? stream.Length : 0, strStyle, "", // strMetadata, strRange, true, baTimeStamp, // timestamp, out baOutputTimestamp, out strOutputPath, out strError); if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.AlreadyExist) { return 1; // 已经存在同名同类型对象 } strError = "写入 '" + strPath + "' 发生错误: " + strError; return -1; } return 0; }
private void button_OK_Click(object sender, System.EventArgs e) { string strMetaData = ""; // TODO: 一旦遇到问题,可以放开注释试验 // this.textBox_content.Text = RemoveSingle0a(this.textBox_content.Text); if (this.Obj != null) { if (this.IsText == true) { byte[] baContent = StringUtil.GetUtf8Bytes(this.textBox_content.Text, true); this.Obj.Content = baContent; } else { if (this.Stream != null) { this.Obj.Content = new byte[this.Stream.Length]; this.Stream.Seek(0, SeekOrigin.Begin); this.Stream.Read(this.Obj.Content, 0, (int)this.Stream.Length); } else { this.Obj.Content = null; } } StringUtil.ChangeMetaData(ref strMetaData, null, // string strID, this.LocalPath, // string strLocalPath, this.Mime, // string strMimeType, null, // string strLastModified, null, // string strPath, null); // string strTimestamp) this.Obj.Metadata = strMetaData; this.Obj.Changed = true; this.DialogResult = DialogResult.OK; this.Close(); return; } this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); DigitalPlatform.Stop stop = null; if (stopManager != null) { stop = new DigitalPlatform.Stop(); stop.Register(this.stopManager, true); // 和容器关联 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存配置文件: " + this.Path); stop.BeginLoop(); } if (this.IsText == true) { // 更新stream对象内容 byte[] baContent = StringUtil.GetUtf8Bytes(this.textBox_content.Text, true); this.Stream = new MemoryStream(baContent); // !!! 什么时候释放? /* this.Stream.SetLength(0); StreamWriter sw = new StreamWriter(this.Stream, Encoding.UTF8); sw.Write(this.textBox_content.Text); */ } if (this.Stream != null) this.Stream.Seek(0, SeekOrigin.Begin); // 保存配置文件 string strError = ""; byte[] baOutputTimestamp = null; string strOutputPath = ""; string strStyle = ""; if (this.checkBox_autoCreate.Checked == true) { if (strStyle != "") strStyle += ","; strStyle += "autocreatedir"; } StringUtil.ChangeMetaData(ref strMetaData, null, // string strID, this.LocalPath, // string strLocalPath, this.Mime, // string strMimeType, null, // string strLastModified, null, // string strPath, null); // string strTimestamp) string strRange = ""; if (this.Stream != null && this.Stream.Length != 0) { Debug.Assert(this.Stream.Length != 0, "test"); strRange = "0-" + Convert.ToString(this.Stream.Length - 1); } long lRet = channel.DoSaveResObject(this.Path, this.Stream, (this.Stream != null && this.Stream.Length != 0) ? this.Stream.Length : 0, strStyle, strMetaData, // strMetadata, strRange, true, this.TimeStamp, // timestamp, out baOutputTimestamp, out strOutputPath, out strError); /* // 保存配置文件 byte[] baOutputTimeStamp = null; string strOutputPath = ""; string strError = ""; long lRet = channel.DoSaveTextRes(this.Path, this.textBox_content.Text, true, // bInlucdePreamble "", // style this.TimeStamp, out baOutputTimeStamp, out strOutputPath, out strError); */ if (stopManager != null) { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); } if (lRet == -1) { MessageBox.Show(this, strError); goto FINISH; } this.TimeStamp = baOutputTimestamp; MessageBox.Show(this, "配置文件 '" + this.Path + "' 保存成功"); ///////////// FINISH: if (stopManager != null && stop != null) { stop.Unregister(); // 和容器关联 stop = null; } this.channel = null; this.DialogResult = DialogResult.OK; this.Close(); }
// 在服务器端创建对象 // return: // -1 错误 // 1 以及存在同名对象 // 0 正常返回 int NewServerSideObject(string strPath, int nType, Stream stream, byte[] baTimeStamp, out byte[] baOutputTimestamp, out string strError) { strError = ""; baOutputTimestamp = null; Debug.Assert(this.Channels != null, ""); this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); DigitalPlatform.Stop stop = null; if (stopManager != null) { stop = new DigitalPlatform.Stop(); stop.Register(this.stopManager, true); // 和容器关联 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在创建新对象: " + this.ServerUrl + "?" + strPath); stop.BeginLoop(); } string strOutputPath = ""; string strStyle = ""; if (nType == ResTree.RESTYPE_FOLDER) strStyle = "createdir"; /* long lRet = channel.DoSaveTextRes(strPath, "", // content 暂时为空 true, // bInlucdePreamble strStyle, // style null, // baTimeStamp, out baOutputTimestamp, out strOutputPath, out strError); */ string strRange = ""; if (stream != null && stream.Length != 0) { Debug.Assert(stream.Length != 0, "test"); strRange = "0-" + Convert.ToString(stream.Length - 1); } long lRet = channel.DoSaveResObject(strPath, stream, (stream != null && stream.Length != 0) ? stream.Length : 0, strStyle, "", // strMetadata, strRange, true, baTimeStamp, // timestamp, out baOutputTimestamp, out strOutputPath, out strError); if (stopManager != null) { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.Unregister(); // 和容器关联 } if (lRet == -1) { if (this.channel.ErrorCode == ChannelErrorCode.AlreadyExist) { this.channel = null; return 1; // 已经存在同名同类型对象 } this.channel = null; strError = "写入 '" + strPath + "' 发生错误: " + strError; return -1; } this.channel = null; return 0; }
// 上载资源 // return: // -1 error // >=0 实际上载的资源对象数 public int DoUpload( string strXmlRecPath, RmsChannel channel, DigitalPlatform.Stop stop, out string strError) { strError = ""; int nUploadCount = 0; string strLastModifyTime = DateTime.UtcNow.ToString("u"); bNotAskTimestampMismatchWhenOverwrite = false; for(int i=0;i<this.Items.Count;i++) { string strState = ListViewUtil.GetItemText(this.Items[i], COLUMN_STATE); string strID = ListViewUtil.GetItemText(this.Items[i], COLUMN_ID); string strResPath = strXmlRecPath + "/object/" + ListViewUtil.GetItemText(this.Items[i], COLUMN_ID); string strLocalFileName = ListViewUtil.GetItemText(this.Items[i], COLUMN_LOCALPATH); string strMime = ListViewUtil.GetItemText(this.Items[i], COLUMN_MIME); string strTimeStamp = ListViewUtil.GetItemText(this.Items[i], COLUMN_TIMESTAMP); if (IsNewFileState(strState) == false) continue; // 检测文件尺寸 FileInfo fi = new FileInfo(strLocalFileName); if (fi.Exists == false) { strError = "文件 '" + strLocalFileName + "' 不存在..."; return -1; } string[] ranges = null; if (fi.Length == 0) { // 空文件 ranges = new string[1]; ranges[0] = ""; } else { string strRange = ""; strRange = "0-" + Convert.ToString(fi.Length-1); // 按照100K作为一个chunk ranges = RangeList.ChunkRange(strRange, 100*1024); } byte [] timestamp = ByteArray.GetTimeStampByteArray(strTimeStamp); byte [] output_timestamp = null; nUploadCount ++; REDOWHOLESAVE: string strWarning = ""; for(int j=0;j<ranges.Length;j++) { REDOSINGLESAVE: Application.DoEvents(); // 出让界面控制权 if (stop.State != 0) { strError = "用户中断"; goto ERROR1; } string strWaiting = ""; if (j == ranges.Length - 1) strWaiting = " 请耐心等待..."; string strPercent = ""; RangeList rl = new RangeList(ranges[j]); if (rl.Count >= 1) { double ratio = (double)((RangeItem)rl[0]).lStart / (double)fi.Length; strPercent = String.Format("{0,3:N}",ratio * (double)100) + "%"; } if (stop != null) stop.SetMessage("正在上载 " + ranges[j] + "/" + Convert.ToString(fi.Length) + " " + strPercent + " " + strLocalFileName + strWarning + strWaiting); /* if (stop != null) stop.SetMessage("正在上载 " + ranges[j] + "/" + Convert.ToString(fi.Length) + " " + strLocalFileName); */ long lRet = channel.DoSaveResObject(strResPath, strLocalFileName, strLocalFileName, strMime, strLastModifyTime, ranges[j], j == ranges.Length - 1 ? true : false, // 最尾一次操作,提醒底层注意设置特殊的WebService API超时时间 timestamp, out output_timestamp, out strError); timestamp = output_timestamp; ListViewUtil.ChangeItemText(this.Items[i], COLUMN_TIMESTAMP, ByteArray.GetHexTimeStampString(timestamp)); strWarning = ""; if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.TimestampMismatch) { if (this.bNotAskTimestampMismatchWhenOverwrite == true) { timestamp = new byte[output_timestamp.Length]; Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length); strWarning = " (时间戳不匹配, 自动重试)"; if (ranges.Length == 1 || j==0) goto REDOSINGLESAVE; goto REDOWHOLESAVE; } DialogResult result = MessageDlg.Show(this, "上载 '" + strLocalFileName + "' (片断:" + ranges[j] + "/总尺寸:"+Convert.ToString(fi.Length) +") 时发现时间戳不匹配。详细情况如下:\r\n---\r\n" + strError + "\r\n---\r\n\r\n是否以新时间戳强行上载?\r\n注:(是)强行上载 (否)忽略当前记录或资源上载,但继续后面的处理 (取消)中断整个批处理", "dp2batch", MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button1, ref this.bNotAskTimestampMismatchWhenOverwrite); if (result == DialogResult.Yes) { timestamp = new byte[output_timestamp.Length]; Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length); strWarning = " (时间戳不匹配, 应用户要求重试)"; if (ranges.Length == 1 || j==0) goto REDOSINGLESAVE; goto REDOWHOLESAVE; } if (result == DialogResult.No) { goto END1; // 继续作后面的资源 } if (result == DialogResult.Cancel) { strError = "用户中断"; goto ERROR1; // 中断整个处理 } } goto ERROR1; } //timestamp = output_timestamp; } DigitalPlatform.Xml.ElementItem item = GetFileItem(strID); if (item != null) { item.SetAttrValue("__state", this.ServerFileState); } else { Debug.Assert(false, "xmleditor中居然不存在id为[" + strID + "]的<dprms:file>元素"); } } END1: if (stop != null) stop.SetMessage("上载资源全部完成"); return nUploadCount; ERROR1: return -1; }
public string CirculationNotifyTypes = ""; // 流通操作时发出实时通知的类型 #if NO // 保存资源 // return: // -1 error // 0 发现上载的文件其实为空,不必保存了 // 1 已经保存 public static int SaveUploadFile( System.Web.UI.Page page, RmsChannel channel, string strXmlRecPath, string strFileID, string strResTimeStamp, HttpPostedFile postedFile, int nLogoLimitW, int nLogoLimitH, out string strError) { strError = ""; if (String.IsNullOrEmpty(postedFile.FileName) == true && postedFile.ContentLength == 0) { return 0; // 没有必要保存 } WebPageStop stop = new WebPageStop(page); string strResPath = strXmlRecPath + "/object/" + strFileID; string strLocalFileName = Path.GetTempFileName(); try { using (Stream t = File.Create(strLocalFileName)) { // 缩小尺寸 int nRet = GraphicsUtil.ShrinkPic(postedFile.InputStream, postedFile.ContentType, nLogoLimitW, nLogoLimitH, true, t, out strError); if (nRet == -1) return -1; if (nRet == 0) // 没有必要缩放 { postedFile.InputStream.Seek(0, SeekOrigin.Begin); // 2012/5/20 StreamUtil.DumpStream(postedFile.InputStream, t); } } // t.Close(); // 检测文件尺寸 FileInfo fi = new FileInfo(strLocalFileName); if (fi.Exists == false) { strError = "文件 '" + strLocalFileName + "' 不存在..."; return -1; } string[] ranges = null; if (fi.Length == 0) { // 空文件 ranges = new string[1]; ranges[0] = ""; } else { string strRange = ""; strRange = "0-" + Convert.ToString(fi.Length - 1); // 按照100K作为一个chunk ranges = RangeList.ChunkRange(strRange, 100 * 1024); } byte[] timestamp = ByteArray.GetTimeStampByteArray(strResTimeStamp); byte[] output_timestamp = null; // 2007/12/13 string strLastModifyTime = DateTime.UtcNow.ToString("u"); string strLocalPath = postedFile.FileName; // page.Response.Write("<br/>正在保存" + strLocalPath); REDOWHOLESAVE: string strWarning = ""; for (int j = 0; j < ranges.Length; j++) { REDOSINGLESAVE: // Application.DoEvents(); // 出让界面控制权 if (stop.State != 0) { strError = "用户中断"; goto ERROR1; } string strWaiting = ""; if (j == ranges.Length - 1) strWaiting = " 请耐心等待..."; string strPercent = ""; RangeList rl = new RangeList(ranges[j]); if (rl.Count >= 1) { double ratio = (double)((RangeItem)rl[0]).lStart / (double)fi.Length; strPercent = String.Format("{0,3:N}", ratio * (double)100) + "%"; } if (stop != null) stop.SetMessage("正在上载 " + ranges[j] + "/" + Convert.ToString(fi.Length) + " " + strPercent + " " + strLocalFileName + strWarning + strWaiting); // page.Response.Write("."); // 防止前端因等待过久而超时 long lRet = channel.DoSaveResObject(strResPath, strLocalFileName, strLocalPath, postedFile.ContentType, strLastModifyTime, ranges[j], j == ranges.Length - 1 ? true : false, // 最尾一次操作,提醒底层注意设置特殊的WebService API超时时间 timestamp, out output_timestamp, out strError); timestamp = output_timestamp; // DomUtil.SetAttr(node, "__timestamp", ByteArray.GetHexTimeStampString(timestamp)); strWarning = ""; if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.TimestampMismatch) { timestamp = new byte[output_timestamp.Length]; Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length); strWarning = " (时间戳不匹配, 自动重试)"; if (ranges.Length == 1 || j == 0) goto REDOSINGLESAVE; goto REDOWHOLESAVE; } goto ERROR1; } } return 1; // 已经保存 ERROR1: return -1; } finally { // 不要忘记删除临时文件 File.Delete(strLocalFileName); } }
// 上载一个res // parameters: // strRecordPath 主记录的路径 // inputfile: 源流 // bIsFirstRes: 是否是第一个资源(xml) // strError: error info // return: // -2 片断中发现时间戳不匹配。本函数调主可重上载整个资源 // -1 error // 0 successed public static int UploadOneRes( IWin32Window owner, Stop stop, RmsChannel channel, ref string strRecordPath, Stream inputfile, OneRes res, // ref DbNameMap map, bool bIsFirstRes, string strCount, ref bool bDontPromptTimestampMismatchWhenOverwrite, out string strError) { strError = ""; int nRet = 0; if (res.Length == 0) { Debug.Assert(false, ""); return 0; // 空包不需上载 } #if NO // 2.为上载做准备 XmlDocument metadataDom = new XmlDocument(); try { metadataDom.LoadXml(res.MetadataXml); } catch (Exception ex) { strError = "加载 metadataxml 到 DOM 时出错: " + ex.Message; goto ERROR1; } XmlNode node = metadataDom.DocumentElement; string strResPath = DomUtil.GetAttr(node, "path"); string strTargetPath = ""; // string strLocalPath = DomUtil.GetAttr(node,"localpath"); // string strMimeType = DomUtil.GetAttr(node,"mimetype"); string strTimeStamp = DomUtil.GetAttr(node, "timestamp"); // 注意,strLocalPath并不是要上载的body文件,它只用来作元数据\ // body文件为strBodyTempFileName #endif // string strTargetPath = strRecordPath; // 3.将body文件拆分成片断进行上载 string[] ranges = null; if (res.Length == 0) { // 空文件 ranges = new string[1]; ranges[0] = ""; } else { string strRange = ""; strRange = "0-" + Convert.ToString(res.Length - 1); // 按照100K作为一个chunk ranges = RangeList.ChunkRange(strRange, 100 * 1024 ); } byte[] timestamp = res.Timestamp; byte[] output_timestamp = null; REDOWHOLESAVE: string strOutputPath = ""; string strWarning = ""; for (int j = 0; j < ranges.Length; j++) { REDOSINGLESAVE: Application.DoEvents(); // 出让界面控制权 if (stop.State != 0) { DialogResult result = MessageBox.Show(owner, "确实要中断当前批处理操作?", "导入数据", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.Yes) { strError = "用户中断"; goto ERROR1; } else { stop.Continue(); } } string strWaiting = ""; if (j == ranges.Length - 1) strWaiting = " 请耐心等待..."; string strPercent = ""; RangeList rl = new RangeList(ranges[j]); if (rl.Count >= 1) { double ratio = (double)((RangeItem)rl[0]).lStart / (double)res.Length; strPercent = String.Format("{0,3:N}", ratio * (double)100) + "%"; } if (stop != null) stop.SetMessage("正在保存 " + ranges[j] + "/" + Convert.ToString(res.Length) + " " + strPercent + " " + strRecordPath + strWarning + strWaiting + " " + strCount); inputfile.Seek(res.StartOffs, SeekOrigin.Begin); long lRet = channel.DoSaveResObject(strRecordPath, inputfile, res.Length, "", // style res.MetadataXml, ranges[j], j == ranges.Length - 1 ? true : false, // 最尾一次操作,提醒底层注意设置特殊的WebService API超时时间 timestamp, out output_timestamp, out strOutputPath, out strError); // stop.SetProgressValue(inputfile.Position); strWarning = ""; if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.TimestampMismatch) { string strDisplayRecPath = strOutputPath; if (string.IsNullOrEmpty(strDisplayRecPath) == true) strDisplayRecPath = strRecordPath; if (bDontPromptTimestampMismatchWhenOverwrite == true) { timestamp = new byte[output_timestamp.Length]; Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length); strWarning = " (时间戳不匹配, 自动重试)"; if (ranges.Length == 1 || j == 0) goto REDOSINGLESAVE; goto REDOWHOLESAVE; } DialogResult result = MessageDlg.Show(owner, "保存 '" + strDisplayRecPath + "' (片断:" + ranges[j] + "/总尺寸:" + Convert.ToString(res.Length) + ") 时发现时间戳不匹配。详细情况如下:\r\n---\r\n" + strError + "\r\n---\r\n\r\n是否以新时间戳强行覆盖保存?\r\n注:\r\n[是] 强行覆盖保存\r\n[否] 忽略当前记录或资源保存,但继续后面的处理\r\n[取消] 中断整个批处理", "导入数据", MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button1, ref bDontPromptTimestampMismatchWhenOverwrite); if (result == DialogResult.Yes) { if (output_timestamp != null) { timestamp = new byte[output_timestamp.Length]; Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length); } else { timestamp = output_timestamp; } strWarning = " (时间戳不匹配, 应用户要求重试)"; if (ranges.Length == 1 || j == 0) goto REDOSINGLESAVE; goto REDOWHOLESAVE; } if (result == DialogResult.No) { return 0; // 继续作后面的资源 } if (result == DialogResult.Cancel) { strError = "用户中断"; goto ERROR1; // 中断整个处理 } } goto ERROR1; } timestamp = output_timestamp; } // 考虑到保存第一个资源的时候,id可能为“?”,因此需要得到实际的id值 if (bIsFirstRes) strRecordPath = strOutputPath; return 0; ERROR1: return -1; }