示例#1
0
文件: Sia.cs 项目: zora/duplicati
        private bool IsDownloadComplete(string siafilename, string localname)
        {
            SiaDownloadList fl = GetDownloads();

            if (fl.Files == null)
            {
                return(false);
            }

            foreach (var f in fl.Files)
            {
                if (f.Siapath == siafilename)
                {
                    if (f.Error != "")
                    {
                        throw new Exception("failed to download " + siafilename + "err: " + f.Error);
                    }
                    if (f.Filesize == f.Received)
                    {
                        try
                        {
                            // Sia seems to keep the file open/locked for a while, make sure we can open it
                            System.IO.FileStream fs = new System.IO.FileStream(localname, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
                            fs.Close();
                        }
                        catch (System.IO.IOException)
                        {
                            return(false);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#2
0
文件: Sia.cs 项目: zora/duplicati
        private SiaDownloadList GetDownloads()
        {
            var    fl       = new SiaDownloadList();
            string endpoint = string.Format("/renter/downloads");

            try
            {
                System.Net.HttpWebRequest req = CreateRequest(endpoint);
                req.Method = System.Net.WebRequestMethods.Http.Get;

                Utility.AsyncHttpRequest areq = new Utility.AsyncHttpRequest(req);

                using (System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)areq.GetResponse())
                {
                    int code = (int)resp.StatusCode;
                    if (code < 200 || code >= 300)
                    {
                        throw new System.Net.WebException(resp.StatusDescription, null, System.Net.WebExceptionStatus.ProtocolError, resp);
                    }

                    var serializer = new JsonSerializer();

                    using (var rs = areq.GetResponseStream())
                        using (var sr = new System.IO.StreamReader(rs))
                            using (var jr = new Newtonsoft.Json.JsonTextReader(sr))
                            {
                                fl = (SiaDownloadList)serializer.Deserialize(jr, typeof(SiaDownloadList));
                            }
                }
            }
            catch (System.Net.WebException wex)
            {
                throw new Exception(getResponseBodyOnError(endpoint, wex));
            }
            return(fl);
        }