Exemplo n.º 1
0
        /// <summary>
        /// Download content of object
        /// </summary>
        /// <param name="destination">Where downloaded data will be saved</param>
        /// <param name="handler">Handler for progress when downloading</param>
        public void Download(Stream destination, RequestProgressHandler handler)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            try
            {
                T obj = Get(Id);
                System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri((obj as AFiles <T>).Source));
                wr.Timeout = 20000;
                System.Net.HttpWebResponse wres = (System.Net.HttpWebResponse)wr.GetResponse();
                byte[] buffer;
                long   read = 0;
                wres.GetResponseStream().CopyTo(destination);

                Stream s = wres.GetResponseStream();
                while (read < wres.ContentLength)
                {
                    int bufferlenght = Convert.ToInt32(wres.ContentLength - read > DownloadBufferSize ? DownloadBufferSize : wres.ContentLength - read);
                    buffer = new byte[bufferlenght];

                    s.Read(buffer, 0, bufferlenght);
                    destination.Write(buffer, 0, bufferlenght);
                    read += buffer.Length;
                    if (handler != null)
                    {
                        handler.ProgressChange((int)read, read / (wres.ContentLength / (double)100));
                    }
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 2
0
 public BitsException(string message, Exception exception, Stream file, long position, BitsRequest request, ContinueHandler handler, RequestProgressHandler progressHandler, object result) : base(message, exception)
 {
     this.Position  = position;
     this.Request   = request;
     this.Content   = file;
     this.Continue += handler;
     this.Handler   = progressHandler;
     Result         = result;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Upload content of object
 /// </summary>
 /// <param name="file">Object content</param>
 /// <param name="usrid">Owner of location. If null or empty logged user is used</param>
 /// <param name="name">Object name</param>
 /// <param name="locationid">Location id where you want to create object</param>
 /// <param name="fposition">Position in stream. It is used when handled exception and continue upload</param>
 /// <param name="handler">Handler which is used for watch upload progress</param>
 /// <param name="br">Bits request object</param>
 /// <param name="resobj">Result object. It is created object given when exception durin uploading process</param>
 static void UploadContent(Stream file, string usrid, string name, string locationid, long fposition, RequestProgressHandler handler, BitsRequest br, object resobj)
 {
     if (br == null)
     {
         Uri url = new Uri("https://cid-" + usrid + ".users.storage.live.com/items/" + locationid + "/" + name);
         br = new BitsRequest {
             Url = url, TotalLength = (int)file.Length
         };
         br.StartSession();
         br.Method = WebRequestMethods.Http.Post;
         try
         {
             Requester.Request <T>(br);
         }
         catch
         { throw; }
     }
     file.Position = fposition;
     while (file.Position < file.Length)
     {
         br.Method = WebRequestMethods.Http.Post;
         long position = file.Position;
         int  c        = br.Count;
         br.SetData(new byte[file.Length - file.Position > br.MaxLength ? br.MaxLength : file.Length - file.Position]);
         file.Read(br.GetData(), 0, br.ContentLength);
         try
         {
             if (handler != null)
             {
                 Requester.Request <T>(br, handler);
             }
             else
             {
                 Requester.Request <T>(br);
             }
         }
         catch (Exception e)
         {
             br.Count = c;
             throw new BitsException("", e, file, position, br, Continue, handler, resobj);
         }
     }
     br.Method = WebRequestMethods.Http.Post;
     br.CommitSession();
     try
     {
         Requester.Request <T>(br);
     }
     catch (Exception e)
     {
         throw new BitsException("", e, file, file.Length, br, Continue, handler, resobj);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Create object with content and progress when upload
        /// </summary>
        /// <param name="locationid">Location id where you want to object create</param>
        /// <param name="name">Object name</param>
        /// <param name="file">Object content</param>
        /// <param name="owoption">Overwrite option</param>
        /// <param name="handler">Progress handler for watch progress when uploading</param>
        /// <returns>Returns created object</returns>
        public static T Create(T fileValue, Stream file, OverwriteOption overwriteOption, RequestProgressHandler handler)
        {
            T          obj     = default(T);
            AFiles <T> fileobj = (fileValue as AFiles <T>);

            if (string.IsNullOrEmpty((fileobj as AFiles <T>).ParentId))
            {
                fileobj.ParentId = "me/skydrive";
            }
            try
            {
                QueryParameters qp = new QueryParameters();
                qp.Add("overwrite", GetOverwriteOption(overwriteOption));
                RequestObject ro = new RequestObject {
                    Url = UrlBuilder1.Build(fileobj.ParentId + "/files/" + fileobj.Name, qp), Method = WebRequestMethods.Http.Put, ContentType = ContentType.ApplicationJsonODataVerbose
                };
                ro.SetData(new byte[] { 0 });
                obj       = Requester.Request <T>(ro).ElementAt(0);
                fileValue = Get((obj as AOperational <T>).Id);
            }
            catch
            {
                throw;
            }
            string[] lid   = fileobj.ParentId.Split('.');
            string   usrid = User.Get("").Id;

            try
            {
                UploadContent(file, usrid, fileobj.Name, lid[lid.Length - 1], 0, handler, null, obj);
            }
            catch
            {
                throw;
            }
            return(obj);
        }