Ejemplo n.º 1
0
        private void Publish()
        {
            // create directories
            var dirs = _newFiles.Select(f => Path.GetDirectoryName(f.SystemPath)).Distinct().ToList();

            foreach (var dir in dirs)
            {
                if (dir == "")
                {
                    continue;
                }

                var ftpReq = (FtpWebRequest)WebRequest.Create("ftp://utopiarealms.com/files/" + dir.Replace('\\', '/'));
                ftpReq.Method      = WebRequestMethods.Ftp.MakeDirectory;
                ftpReq.Credentials = new NetworkCredential("update", "xe6ORAXoNO");
                try
                {
                    var response = (FtpWebResponse)ftpReq.GetResponse();
                    Console.WriteLine("Create dir complete, status {0}", response.StatusDescription);
                    response.Close();
                }
                catch (WebException)
                {
                }
            }

            int index = 0;

            // upload files
            foreach (var updateFileInfo in _newFiles)
            {
                var url = "ftp://update.utopiarealms.com/files/" + updateFileInfo.SystemPath.Replace('\\', '/');
                _file     = updateFileInfo.SystemPath;
                _progress = (float)index / _newFiles.Count;
                using (var fs = File.OpenRead(Path.Combine(_baseFolderPath, updateFileInfo.SystemPath)))
                {
                    UploadFile(url, fs);
                }
                index++;
            }

            // upload index file
            _file = "index file...";
            using (var ms = new MemoryStream())
            {
                using (var zs = new GZipStream(ms, CompressionMode.Compress))
                {
                    var bwriter = new BinaryWriter(zs);
                    _updateFile.Save(bwriter);
                }
                var ms2 = new MemoryStream(ms.ToArray());
                UploadFile("ftp://update.utopiarealms.com/index.update", ms2);
            }

            using (var ms = new MemoryStream())
            {
                using (var writer = new StreamWriter(ms, Encoding.UTF8))
                {
                    writer.Write(_updateFile.UpdateToken);
                    writer.Flush();
                    UploadFile("ftp://update.utopiarealms.com/token", ms);
                }
            }
            _finished = true;
        }