示例#1
0
        private bool Download(CacheContentFile file)
        {
            var fileName        = Path.Combine(Location, file.Name);
            var outputDirectory = Path.GetDirectoryName(fileName);
            var temporaryFile   = Path.Combine(outputDirectory, file.SHA1Hash + ".cpart");
            var URL             = _server + file.Cache + "/" + file.Name;

            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }
            int startAt = 0;

            Log.Info(String.Format("Starting to download {0}, {1} bytes.", file.Name, file.Size));
            if (File.Exists(temporaryFile))
            {
                //resume the download
                if (_usecpart)
                {
                    var fi = new FileInfo(temporaryFile);
                    startAt = (int)fi.Length;
                    SendStatus(StatusChangedEnum.FileDownloading, new object[] { file.Name, file.CompressedSize, startAt }, startAt);
                    Log.Info(String.Format("Partial file for {1} found, starting download at {0} bytes.", startAt, file.Name));
                }
                else
                {
                    File.Delete(temporaryFile);
                }
            }
            bool compressed = false;

#if !DEBUG
            try
            {
#endif
            if (Utilities.URLExists(URL + ".lzma"))
            {
                compressed = true;
                URL       += ".lzma";
                SendStatus(StatusChangedEnum.FileDownloadingLzma, file.Name, 0);
                Log.Info(String.Format("Found a compressed file for {0}.", fileName));
            }
            Stream inStream, outStream;
            if (startAt != file.Size && startAt != file.CompressedSize)     //if they equal, ._.
            {
                // initialize request
                var webRequest = (HttpWebRequest)WebRequest.Create(URL);
                webRequest.AddRange(startAt);

                var webResponse = (HttpWebResponse)webRequest.GetResponse();

                inStream  = webResponse.GetResponseStream();
                outStream = File.Open(temporaryFile, FileMode.Append, FileAccess.Write, FileShare.Read);

                var buffer    = new byte[2048];
                var lastBytes = 0;

                while ((lastBytes = inStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, lastBytes);
                    SendStatus(StatusChangedEnum.FileDownloading, new object[] { file.Name, file.CompressedSize, lastBytes }, lastBytes);
                }

                // clean up the streams
                inStream.Close();
                outStream.Close();
                SendStatus(StatusChangedEnum.FileDownloadFinish, file.Name, 0);
            }
            if (compressed)
            {
                try
                {
                    // decompress the temp file
                    var tempDecPath = fileName + ".deco";
                    var decoder     = new LZMACoder();
                    SendStatus(StatusChangedEnum.FileDecompressStart, file.Name, 0);
                    Log.Info("decompressing " + file.Name);

                    inStream  = File.OpenRead(temporaryFile);
                    outStream = File.Open(tempDecPath, FileMode.Create);

                    try
                    {
                        decoder.Decompress(inStream, outStream);
                    }
                    catch (ApplicationException ex)
                    {
                        inStream.Close();
                        outStream.Close();

                        throw ex;
                    }

                    inStream.Close();
                    outStream.Close();

                    File.Delete(temporaryFile);
                    File.Move(tempDecPath, temporaryFile);
                    SendStatus(StatusChangedEnum.FileDecompressFinish, file.Name, 0);
                }
                catch (ApplicationException e)
                {
                    // lzma sdk threw an error.
                    File.Delete(temporaryFile);
                    Log.Error(String.Format("LZMA file failed to decompress. {0}", e.ToString()));
                    _lastException = e;
                    return(false);
                }
            }
            // check the temp file's validity
            SendStatus(StatusChangedEnum.FileVerifyStart, file.Name, 0);
            var outHash = Utilities.GetFileSHA1(temporaryFile);
            var inHash  = file.SHA1Hash;

            if (inHash != outHash)
            {
                _lastException = new Exception("Hash verification failed for " + fileName);
                Log.Error("Hash verification failed for " + fileName);
                File.Delete(temporaryFile);
                SendStatus(StatusChangedEnum.FileVerifyFailed, file.Name, 0);
                return(false);
            }
            SendStatus(StatusChangedEnum.FileVerifyFinish, file.Name, 0);
            // and rename the temp file, and then delete it
            File.Copy(temporaryFile, fileName, true);
            File.Delete(temporaryFile);
            return(true);

#if !DEBUG
        }

        catch (Exception e) {
            Log.Error(String.Format("Error while downloading {0}.", e.ToString()));
            _lastException = e;
        }
        return(false);
#endif
        }
示例#2
0
        private bool Download(CacheContentFile file)
        {
            var fileName = Path.Combine(Location, file.Name.Replace("\\", "/"));
            var outputDirectory = Path.GetDirectoryName(fileName);
            var temporaryFile = Path.Combine(outputDirectory, file.SHA1Hash + ".cpart");
            var URL = _server + file.Cache + "/" + file.Name;
            if (!Directory.Exists(outputDirectory)) Directory.CreateDirectory(outputDirectory);
            int startAt = 0;

            Log.Info(String.Format("Starting to download {0}, {1} bytes.", file.Name, file.Size));
            if(File.Exists(temporaryFile))
            {
                //resume the download
                if(_usecpart)
                {
                    var fi = new FileInfo(temporaryFile);
                    startAt = (int) fi.Length;

                    Log.Info(String.Format("Partial file for {1} found, starting download at {0} bytes.", startAt, file.Name));
                }
                else
                {
                    File.Delete(temporaryFile);
                }
            }
            bool compressed = false;
            #if !DEBUG
            try
            {
            #endif
                if(Utilities.URLExists(URL + ".lzma"))
                {
                    compressed = true;
                    URL += ".lzma";
                    SendStatus(StatusChangedEnum.FileDownloadingLzma, file.Name, 0);
                    Log.Info(String.Format("Found a compressed file for {0}.", fileName));
                }
                Stream inStream, outStream;
                if (startAt != file.Size && startAt != file.CompressedSize) //if they equal, ._.
                {
                    // initialize request
                    var webRequest = (HttpWebRequest) WebRequest.Create(URL);
                    webRequest.AddRange(startAt);

                    var webResponse = (HttpWebResponse) webRequest.GetResponse();

                    inStream = webResponse.GetResponseStream();
                    outStream = File.Open(temporaryFile, FileMode.Append, FileAccess.Write, FileShare.Read);

                    var buffer = new byte[2048];
                    var lastBytes = 0;
                    bool Mode = false;

                    while ((lastBytes = inStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        outStream.Write(buffer, 0, lastBytes);
                        if (!Mode)
                        {
                            SendStatus(StatusChangedEnum.FileDownloading, new object[] { file.Name, file.CompressedSize, lastBytes }, lastBytes);
                            Mode = true;
                        }
                    }

                    // clean up the streams
                    Mode = false;
                    inStream.Close();
                    outStream.Close();
                    SendStatus(StatusChangedEnum.FileDownloadFinish, file.Name, 0);
                }
                if (compressed)
                {
                    try
                    {
                        // decompress the temp file
                        var tempDecPath = fileName + ".deco";
                        var decoder = new LZMACoder();
                        SendStatus(StatusChangedEnum.FileDecompressStart, file.Name, 0);
                        Log.Info("decompressing " + file.Name);

                        inStream = File.OpenRead(temporaryFile);
                        outStream = File.Open(tempDecPath, FileMode.Create);

                        try
                        {
                            decoder.Decompress(inStream, outStream);
                        }
                        catch (ApplicationException ex)
                        {
                            inStream.Close();
                            outStream.Close();

                            throw ex;
                        }

                        inStream.Close();
                        outStream.Close();

                        File.Delete(temporaryFile);
                        File.Move(tempDecPath, temporaryFile);
                        SendStatus(StatusChangedEnum.FileDecompressFinish, file.Name, 0);
                    }
                    catch (ApplicationException e)
                    {
                        // lzma sdk threw an error.
                        File.Delete(temporaryFile);
                        Log.Error(String.Format("LZMA file failed to decompress. {0}", e.ToString()));
                        _lastException = e;
                        return false;
                    }
                }
                // check the temp file's validity
                SendStatus(StatusChangedEnum.FileVerifyStart, file.Name, 0);
                var outHash = Utilities.GetFileSHA1(temporaryFile);
                var inHash = file.SHA1Hash;

                if (inHash != outHash)
                {
                    _lastException = new Exception("Hash verification failed for " + fileName);
                    Log.Error("Hash verification failed for " + fileName);
                    File.Delete(temporaryFile);
                    SendStatus(StatusChangedEnum.FileVerifyFailed, file.Name, 0);
                    return false;
                }
                SendStatus(StatusChangedEnum.FileVerifyFinish, file.Name, 0);
                // and rename the temp file, and then delete it
                File.Copy(temporaryFile, fileName, true);
                File.Delete(temporaryFile);
                return true;
            #if !DEBUG
            }
            catch (Exception e) {
                Log.Error(String.Format("Error while downloading {0}.", e.ToString()));
                _lastException = e;
            }
            return false;
            #endif
        }