Exemplo n.º 1
0
 public override void Disconnect()
 {
     try
     {
         if (_fileDestination != null)
         {
             _fileDestination.Disconnect();
             _fileDestination.Dispose();
             _fileDestination = null;
         }
     }
     catch (Exception ex)
     {
         WebPublishMessage message = WebPublishUtils.ExceptionToErrorMessage(ex);
         throw new BlogClientFileTransferException("disconnecting from file destination", message.Text, message.Title);
     }
 }
Exemplo n.º 2
0
        public override Uri DoUploadWorkBeforePublish(IFileUploadContext uploadContext)
        {
            try
            {
                ConnectForUpload();

                string uploadPath = uploadContext.Settings.GetString(UPLOAD_PATH, null);
                bool   overwrite  = uploadPath != null;
                if (uploadPath == null)
                {
                    string   uploadFolder = null;
                    string   fileName     = uploadContext.PreferredFileName;
                    string   filePath     = uploadContext.FormatFileName(fileName);
                    string[] pathParts    = filePath.Split('/');
                    if (pathParts.Length > 1)
                    {
                        uploadFolder = FileHelper.GetValidAnsiFileName(pathParts[0]);
                        for (int i = 1; i < pathParts.Length - 1; i++)
                        {
                            uploadFolder = uploadFolder + "/" + FileHelper.GetValidAnsiFileName(pathParts[i]);
                        }
                    }

                    fileName   = FileHelper.GetValidAnsiFileName(pathParts[pathParts.Length - 1]);
                    uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                    if (_fileDestination.FileExists(uploadPath))
                    {
                        string fileBaseName  = Path.GetFileNameWithoutExtension(fileName);
                        string fileExtension = Path.GetExtension(fileName);
                        try
                        {
                            Hashtable existingFiles = new Hashtable();
                            foreach (string name in  _fileDestination.ListFiles(uploadFolder))
                            {
                                existingFiles[name] = name;
                            }
                            for (int i = 3; i < Int32.MaxValue && existingFiles.ContainsKey(fileName); i++)
                            {
                                fileName = FileHelper.GetValidAnsiFileName(fileBaseName + "_" + i + fileExtension);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Fail("Error while calculating unique filename", e.ToString());
                        }

                        uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                        if (_fileDestination.FileExists(uploadPath))
                        {
                            Debug.Fail("Failed to calculate unique filename");
                            fileName   = FileHelper.GetValidAnsiFileName(fileBaseName + Guid.NewGuid().ToString() + fileExtension);
                            uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                        }
                    }
                }

                // transfer the file
                _fileDestination.DoTransfer(
                    uploadContext.GetContentsLocalFilePath(),
                    uploadPath,
                    overwrite);

                uploadContext.Settings.SetString(UPLOAD_PATH, uploadPath);

                // return the url to the transferred file
                string baseUrl     = UrlHelper.InsureTrailingSlash(_settings.UrlMapping);
                string relativeUrl = uploadPath;
                return(new Uri(UrlHelper.UrlCombine(baseUrl, relativeUrl)));
            }
            catch (Exception ex)
            {
                WebPublishMessage message = WebPublishUtils.ExceptionToErrorMessage(ex);
                throw new BlogClientFileTransferException(new FileInfo(uploadContext.GetContentsLocalFilePath()), message.Title, message.Text);
            }
        }
Exemplo n.º 3
0
        private void ConnectForUpload()
        {
            if (_fileDestination == null)
            {
                try
                {
                    bool loggedIn = false;

                    FtpCredentials credentials = (FtpCredentials)_credentials[DestinationContext];
                    string         username    = credentials != null ? credentials.Username : _settings.Username;
                    string         password    = credentials != null ? credentials.Password : _settings.Password;

                    while (!loggedIn)
                    {
                        if (password == String.Empty)
                        {
                            CredentialsDomain       cd     = new CredentialsDomain(Res.Get(StringId.FtpLoginDomain), _settings.FtpServer, null, FtpIconBytes);
                            CredentialsPromptResult result = CredentialsHelper.PromptForCredentials(ref username, ref password, cd);
                            if (result == CredentialsPromptResult.Cancel || result == CredentialsPromptResult.Abort)
                            {
                                throw new OperationCancelledException();
                            }
                            else
                            {
                                //save the user/pass as appropriate
                                if (result == CredentialsPromptResult.SaveUsername)
                                {
                                    _settings.Username = username;
                                    _settings.Password = String.Empty;
                                }
                                else if (result == CredentialsPromptResult.SaveUsernameAndPassword)
                                {
                                    _settings.Username = username;
                                    _settings.Password = password;
                                }
                            }
                        }
                        try
                        {
                            // create and connect to the destination
                            _fileDestination = new WinInetFTPFileDestination(
                                _settings.FtpServer,
                                _settings.PublishPath,
                                username,
                                password);

                            _fileDestination.Connect();

                            //save the validated credentials so we don't need to prompt again later
                            _credentials[DestinationContext] = new FtpCredentials(DestinationContext, username, password);

                            loggedIn = true;
                        }
                        catch (LoginException)
                        {
                            loggedIn = false;
                            password = String.Empty;
                            _credentials.Remove(DestinationContext);
                        }
                    }

                    // calculate the target path and ensure that it exists
                    _fileDestination.InsureDirectoryExists(PostContext);
                }
                catch (Exception ex)
                {
                    WebPublishMessage message = WebPublishUtils.ExceptionToErrorMessage(ex);
                    throw new BlogClientFileTransferException(Res.Get(StringId.BCEFileTransferConnectingToDestination), message.Title, message.Text);
                }
            }
        }