Exemple #1
0
        /// <summary>
        /// Uploads the specified file to the shard website
        /// </summary>
        /// <param name="fileName">the absolute or relative path of the file to be uploaded</param>
        /// <param name="uploadPath">the relative path on the web server to upload the file to</param>
        /// <param name="asciiFormat">if true, upload in ASCII format; if false, upload in binary format</param>
        /// <param name="deleteAfterUpload">if true, deletes the given file after uploading</param>
        public void UploadFile(string fileName, string uploadPath, bool asciiFormat, bool deleteAfterUpload)
        {
            if (!CollectRegistrySettings(out _ftpHost, out _ftpUsername, out _ftpPassword))
            {
                ExceptionManager.LogException("FTPManager", new Exception("Unable to collect the necessary registry settings to upload " + fileName));
            }
            else if (!File.Exists(fileName))
            {
                ExceptionManager.LogException("FTPManager", new FileNotFoundException("The specified file was not found.", fileName));
            }
            else
            {
                _currentRequest  = new UploadRequest(fileName, uploadPath, asciiFormat, deleteAfterUpload);
                _instructionFile = DateTime.Now.Ticks.ToString() + ".ftp";

                if (_processLocked || File.Exists(_instructionFile))
                {
                    _requestQueue.Enqueue(_currentRequest);
                }
                else
                {
                    try
                    {
                        using (StreamWriter writer = new StreamWriter(_instructionFile))
                        {
                            writer.AutoFlush = true;

                            writer.WriteLine("open {0}", _ftpHost);
                            writer.WriteLine(_ftpUsername);
                            writer.WriteLine(_ftpPassword);
                            writer.WriteLine("cd {0}", _currentRequest.UploadPath);
                            writer.WriteLine(_currentRequest.ASCIIFormat ? "ascii" : "binary");
                            writer.WriteLine("put \"{0}\"", _currentRequest.FileName);
                            writer.WriteLine("close");
                            writer.Write("quit");

                            writer.Close();
                        }

                        _timeoutDelay = (IsArchivePackage(fileName) ? 180 : 60);

                        _ftpProc = new Process();
                        _ftpProc.EnableRaisingEvents = true;
                        _ftpProc.Exited               += new EventHandler(delegate(object sender, EventArgs args) { CleanUp(); });
                        _ftpProc.StartInfo             = new ProcessStartInfo("ftp", String.Format("-v -i -s:\"{0}\"", _instructionFile));
                        _ftpProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                        _processLocked = true;
                        _ftpProc.Start();

                        int elapsedTime = 0;
                        while (_processLocked)
                        {
                            elapsedTime += 100;

                            if (elapsedTime >= (_timeoutDelay * 1000))
                            {
                                CleanUp();
                                break;
                            }

                            Thread.Sleep(100);
                        }
                    }
                    catch (IOException ioe)
                    {
                        ExceptionManager.LogException("FTPManager.UploadFile(): Unable to upload file \"" + fileName + "\":\n", ioe);
                        CleanUp();
                    }
                    catch (Exception e)
                    {
                        ExceptionManager.LogException("FTPManager.UploadFile(): Unable to upload file \"" + fileName + "\":\n", e);
                        CleanUp();
                    }
                }
            }
        }