当SaveBinaryDirect遇到网站配额模版

根据某个“众所周知”的原因,当我们使用SharePoint的CSOM(客户端对象模型)上载文件的时候,推荐使用SaveBinaryDirect取代FileCollection.Add的方法(详细原因可以参考:Uploading files using Client Object Model in SharePoint 2010)。但是今天发现,如果网站集设置了配额模版,并且网站的使用空间已经达到/超过存储配额的时候,SaveBinaryDirect在某种情况下会出现奇怪的行为:

如果我们使用overwrite参数的那个重载的时候,如果overwrite为true(强行覆盖服务器端同名文件),如果超出了配额限制,会产生一个代号507的WebException,提示我们空间不足(这个正常);但是当我们使用overwrite为false的时候,会产生一个ClientRequestException,并提示我们已经存在同名文件(这就很奇怪了)。

通过对Microsoft.SharePoint.Client.dll反编译,我们会发现SaveBinaryDirect某些情况下并没有考虑到这种情况:

image

红框那个位置就是这个异常的来源,可以看到只要HttpStatusCode不是PreconditionFailed(412),就会返回文件名已存在的异常(使用ETag的是另外一个重载)。

根据实际测试可以发现,当网站集已经达到/超过配额的时候,和文件重名的时候,Http状态码确实都是412,这两种情况下唯一的分别就是在Response的Header中,一个名字叫X-MSDAVEXT_Error的头,前者的时候是“589923”后面跟着详细的错误描述(就是我们从页面中上载文档时看到的那个已达到配额的异常文字),后者是589951后面跟着详细的错误信息。实际上,我们可以从这篇文档([MS-WEBDAV]: Service Error Code)中看到这两种情况分别是“Quota Exceeded”和“A file with the same name exists”。

因此只能说微软在做这个API的时候没有考虑到这种情况,所以如果需要的话,我们只能自己重写这个方法(中间会用到一次反射调用一个internal方法),代码如下:

   1: public static void SaveFileDirectWithoutOverwrite(ClientContext ctx, string serverRelUrl, Stream stream)
   2: {
   3:     string absolutePath = (new Uri(new Uri(ctx.Url), serverRelUrl)).AbsoluteUri;
   4:  
   5:     WebRequestExecutor webRequestExecutor = ctx.WebRequestExecutorFactory.CreateWebRequestExecutor(ctx, absolutePath);
   6:     webRequestExecutor.RequestKeepAlive = false;
   7:     webRequestExecutor.RequestMethod = "PUT";
   8:     webRequestExecutor.RequestHeaders[HttpRequestHeader.IfNoneMatch] = "*";
   9:  
  10:     Stream requestStream = webRequestExecutor.GetRequestStream();
  11:     byte[] numArray = new byte[0x400];
  12:     while (true)
  13:     {
  14:         int num = stream.Read(numArray, 0, 0x400);
  15:         int num1 = num;
  16:         if (num <= 0)
  17:         {
  18:             break;
  19:         }
  20:         requestStream.Write(numArray, 0, num1);
  21:     }
  22:     requestStream.Flush();
  23:     requestStream.Close();
  24:  
  25:     var method = typeof(ClientContext).GetMethod("FireExecutingWebRequestEventInternal",
  26:         BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding);
  27:     method.Invoke(ctx, new object[] { new WebRequestEventArgs(webRequestExecutor) });
  28:  
  29:     try
  30:     {
  31:         webRequestExecutor.Execute();
  32:         if (webRequestExecutor.StatusCode != HttpStatusCode.Created && webRequestExecutor.StatusCode != HttpStatusCode.OK)
  33:         {
  34:             object[] responseContentType = new object[2];
  35:             responseContentType[0] = webRequestExecutor.ResponseContentType;
  36:             responseContentType[1] = webRequestExecutor.StatusCode;
  37:             throw new ClientRequestException(Resources.GetString("RequestUnexpectedResponse", responseContentType));
  38:         }
  39:     }
  40:     catch (WebException webEx)
  41:     {
  42:         WebException webException = webEx;
  43:         HttpWebResponse response = webException.Response as HttpWebResponse;
  44:         if (response == null || response.StatusCode != HttpStatusCode.PreconditionFailed)
  45:         {
  46:             throw;
  47:         }
  48:         else if (response.Headers["X-MSDAVEXT_Error"] != null && response.Headers["X-MSDAVEXT_Error"].StartsWith("589923;"))
  49:         {
  50:             throw new Exceptions.ServerFullException();
  51:         }
  52:         else
  53:         {
  54:             throw new ClientRequestException(Resources.GetString("FileAlreadyExists"));
  55:         }
  56:     }
  57: }

和SharePoint自己的代码相比,只是在异常捕获中增加了一个判断,其中的那个ServerFullException是我自己定义的异常类,可以根据需要进行改动。

posted on 2012-12-07 23:14  Erucy  阅读(696)  评论(0编辑  收藏  举报

导航