public override UploadResult Upload(Stream stream, string fileName)
        {
            UploadResult result = new UploadResult();

            string remotePath = GetRemotePath(fileName);

            try
            {
                stream.Position = 0;

                if (FTPAccount.Protocol == FTPProtocol.SFTP)
                {
                    using (SFTP sftpClient = new SFTP(FTPAccount))
                    {
                        if (!sftpClient.IsInstantiated)
                        {
                            Errors.Add("An SFTP client couldn't be instantiated, not enough information.\r\nCould be a missing key file.");
                        }
                        else
                        {
                            sftpClient.ProgressChanged += new Uploader.ProgressEventHandler(x => OnProgressChanged(x));
                            sftpClient.UploadData(stream, remotePath);
                        }
                    }
                }
                else // FTP or FTPS
                {
                    using (FTP ftpClient = new FTP(FTPAccount))
                    {
                        ftpClient.ProgressChanged += new Uploader.ProgressEventHandler(x => OnProgressChanged(x));
                        ftpClient.UploadData(stream, remotePath);
                    }
                }
            }
            catch (Exception e)
            {
                DebugHelper.WriteException(e);
                Errors.Add(e.Message);
            }

            if (Errors.Count == 0)
            {
                result.URL = FTPAccount.GetUriPath(fileName);
            }

            return result;
        }
Exemple #2
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            UploadResult result = new UploadResult();

            string remotePath = GetRemotePath(fileName);

            try
            {
                stream.Position = 0;

                if (FTPAccount.Protocol == FTPProtocol.SFTP)
                {
                    using (SFTP sftpClient = new SFTP(FTPAccount))
                    {
                        if (!sftpClient.IsInstantiated)
                        {
                            Errors.Add("An SFTP client couldn't be instantiated, not enough information.\r\nCould be a missing key file.");
                        }
                        else
                        {
                            sftpClient.ProgressChanged += new Uploader.ProgressEventHandler(x => OnProgressChanged(x));
                            sftpClient.UploadData(stream, remotePath);
                        }
                    }
                }
                else // FTP or FTPS
                {
                    using (FTP ftpClient = new FTP(FTPAccount))
                    {
                        ftpClient.ProgressChanged += new Uploader.ProgressEventHandler(x => OnProgressChanged(x));
                        ftpClient.UploadData(stream, remotePath);
                    }
                }
            }
            catch (Exception e)
            {
                DebugHelper.WriteException(e);
                Errors.Add(e.Message);
            }

            if (Errors.Count == 0)
            {
                result.URL = FTPAccount.GetUriPath(fileName);
            }

            return(result);
        }
Exemple #3
0
        public UploadResult UploadFile(Stream stream, string fileName)
        {
            FileUploader fileUploader = null;

            FileDestination fileDestination;

            switch (Info.DataType)
            {
                case EDataType.Image:
                    fileDestination = Info.TaskSettings.ImageFileDestination;
                    break;
                case EDataType.Text:
                    fileDestination = Info.TaskSettings.TextFileDestination;
                    break;
                default:
                case EDataType.File:
                    fileDestination = Info.TaskSettings.FileDestination;
                    break;
            }

            switch (fileDestination)
            {
                case FileDestination.Dropbox:
                    fileUploader = new Dropbox(Program.UploadersConfig.DropboxOAuth2Info, Program.UploadersConfig.DropboxAccountInfo)
                    {
                        UploadPath = NameParser.Parse(NameParserType.URL, Dropbox.TidyUploadPath(Program.UploadersConfig.DropboxUploadPath)),
                        AutoCreateShareableLink = Program.UploadersConfig.DropboxAutoCreateShareableLink,
                        ShareURLType = Program.UploadersConfig.DropboxURLType
                    };
                    break;
                case FileDestination.OneDrive:
                    fileUploader = new OneDrive(Program.UploadersConfig.OneDriveOAuth2Info);
                    break;
                case FileDestination.Copy:
                    fileUploader = new Copy(Program.UploadersConfig.CopyOAuthInfo, Program.UploadersConfig.CopyAccountInfo)
                    {
                        UploadPath = NameParser.Parse(NameParserType.URL, Copy.TidyUploadPath(Program.UploadersConfig.CopyUploadPath)),
                        URLType = Program.UploadersConfig.CopyURLType
                    };
                    break;
                case FileDestination.GoogleDrive:
                    fileUploader = new GoogleDrive(Program.UploadersConfig.GoogleDriveOAuth2Info)
                    {
                        IsPublic = Program.UploadersConfig.GoogleDriveIsPublic,
                        FolderID = Program.UploadersConfig.GoogleDriveUseFolder ? Program.UploadersConfig.GoogleDriveFolderID : null
                    };
                    break;
                case FileDestination.RapidShare:
                    fileUploader = new RapidShare(Program.UploadersConfig.RapidShareUsername, Program.UploadersConfig.RapidSharePassword, Program.UploadersConfig.RapidShareFolderID);
                    break;
                case FileDestination.SendSpace:
                    fileUploader = new SendSpace(APIKeys.SendSpaceKey);
                    switch (Program.UploadersConfig.SendSpaceAccountType)
                    {
                        case AccountType.Anonymous:
                            SendSpaceManager.PrepareUploadInfo(APIKeys.SendSpaceKey);
                            break;
                        case AccountType.User:
                            SendSpaceManager.PrepareUploadInfo(APIKeys.SendSpaceKey, Program.UploadersConfig.SendSpaceUsername, Program.UploadersConfig.SendSpacePassword);
                            break;
                    }
                    break;
                case FileDestination.Minus:
                    fileUploader = new Minus(Program.UploadersConfig.MinusConfig, Program.UploadersConfig.MinusOAuth2Info);
                    break;
                case FileDestination.Box:
                    fileUploader = new Box(Program.UploadersConfig.BoxOAuth2Info)
                    {
                        FolderID = Program.UploadersConfig.BoxSelectedFolder.id,
                        Share = Program.UploadersConfig.BoxShare
                    };
                    break;
                case FileDestination.Gfycat:
                    fileUploader = new GfycatUploader();
                    break;
                case FileDestination.Ge_tt:
                    fileUploader = new Ge_tt(APIKeys.Ge_ttKey)
                    {
                        AccessToken = Program.UploadersConfig.Ge_ttLogin.AccessToken
                    };
                    break;
                case FileDestination.Localhostr:
                    fileUploader = new Hostr(Program.UploadersConfig.LocalhostrEmail, Program.UploadersConfig.LocalhostrPassword)
                    {
                        DirectURL = Program.UploadersConfig.LocalhostrDirectURL
                    };
                    break;
                case FileDestination.CustomFileUploader:
                    if (Program.UploadersConfig.CustomUploadersList.IsValidIndex(Program.UploadersConfig.CustomFileUploaderSelected))
                    {
                        fileUploader = new CustomFileUploader(Program.UploadersConfig.CustomUploadersList[Program.UploadersConfig.CustomFileUploaderSelected]);
                    }
                    break;
                case FileDestination.FTP:
                    int index;

                    if (Info.TaskSettings.OverrideFTP)
                    {
                        index = Info.TaskSettings.FTPIndex.BetweenOrDefault(0, Program.UploadersConfig.FTPAccountList.Count - 1);
                    }
                    else
                    {
                        index = Program.UploadersConfig.GetFTPIndex(Info.DataType);
                    }

                    FTPAccount account = Program.UploadersConfig.FTPAccountList.ReturnIfValidIndex(index);

                    if (account != null)
                    {
                        if (account.Protocol == FTPProtocol.FTP || account.Protocol == FTPProtocol.FTPS)
                        {
                            fileUploader = new FTP(account);
                        }
                        else if (account.Protocol == FTPProtocol.SFTP)
                        {
                            fileUploader = new SFTP(account);
                        }
                    }
                    break;
                case FileDestination.SharedFolder:
                    int idLocalhost = Program.UploadersConfig.GetLocalhostIndex(Info.DataType);
                    if (Program.UploadersConfig.LocalhostAccountList.IsValidIndex(idLocalhost))
                    {
                        fileUploader = new SharedFolderUploader(Program.UploadersConfig.LocalhostAccountList[idLocalhost]);
                    }
                    break;
                case FileDestination.Email:
                    using (EmailForm emailForm = new EmailForm(Program.UploadersConfig.EmailRememberLastTo ? Program.UploadersConfig.EmailLastTo : string.Empty,
                        Program.UploadersConfig.EmailDefaultSubject, Program.UploadersConfig.EmailDefaultBody))
                    {
                        emailForm.Icon = ShareXResources.Icon;

                        if (emailForm.ShowDialog() == DialogResult.OK)
                        {
                            if (Program.UploadersConfig.EmailRememberLastTo)
                            {
                                Program.UploadersConfig.EmailLastTo = emailForm.ToEmail;
                            }

                            fileUploader = new Email
                            {
                                SmtpServer = Program.UploadersConfig.EmailSmtpServer,
                                SmtpPort = Program.UploadersConfig.EmailSmtpPort,
                                FromEmail = Program.UploadersConfig.EmailFrom,
                                Password = Program.UploadersConfig.EmailPassword,
                                ToEmail = emailForm.ToEmail,
                                Subject = emailForm.Subject,
                                Body = emailForm.Body
                            };
                        }
                        else
                        {
                            StopRequested = true;
                        }
                    }
                    break;
                case FileDestination.Jira:
                    fileUploader = new Jira(Program.UploadersConfig.JiraHost, Program.UploadersConfig.JiraOAuthInfo, Program.UploadersConfig.JiraIssuePrefix);
                    break;
                case FileDestination.Mega:
                    fileUploader = new Mega(Program.UploadersConfig.MegaAuthInfos, Program.UploadersConfig.MegaParentNodeId);
                    break;
                case FileDestination.AmazonS3:
                    fileUploader = new AmazonS3(Program.UploadersConfig.AmazonS3Settings);
                    break;
                case FileDestination.OwnCloud:
                    fileUploader = new OwnCloud(Program.UploadersConfig.OwnCloudHost, Program.UploadersConfig.OwnCloudUsername, Program.UploadersConfig.OwnCloudPassword)
                    {
                        Path = Program.UploadersConfig.OwnCloudPath,
                        CreateShare = Program.UploadersConfig.OwnCloudCreateShare,
                        DirectLink = Program.UploadersConfig.OwnCloudDirectLink
                    };
                    break;
                case FileDestination.Pushbullet:
                    fileUploader = new Pushbullet(Program.UploadersConfig.PushbulletSettings);
                    break;
                case FileDestination.MediaCrush:
                    fileUploader = new MediaCrushUploader();
                    break;
                case FileDestination.MediaFire:
                    fileUploader = new MediaFire(APIKeys.MediaFireAppId, APIKeys.MediaFireApiKey, Program.UploadersConfig.MediaFireUsername, Program.UploadersConfig.MediaFirePassword)
                    {
                        UploadPath = NameParser.Parse(NameParserType.URL, Program.UploadersConfig.MediaFirePath),
                        UseLongLink = Program.UploadersConfig.MediaFireUseLongLink
                    };
                    break;
            }

            if (fileUploader != null)
            {
                PrepareUploader(fileUploader);
                return fileUploader.Upload(stream, fileName);
            }

            return null;
        }
        public static void TestFTPAccount(FTPAccount account, bool silent)
        {
            string msg = string.Empty;
            string sfp = account.GetSubFolderPath();

            switch (account.Protocol)
            {
                case FTPProtocol.SFTP:
                    try
                    {
                        using (SFTP sftp = new SFTP(account))
                        {
                            if (!sftp.IsValidAccount)
                            {
                                msg = "An SFTP client couldn't be instantiated, not enough information.\r\nCould be a missing key file.";
                            }
                            else if (sftp.Connect())
                            {
                                List<string> createddirs = new List<string>();

                                if (!sftp.DirectoryExists(sfp))
                                {
                                    createddirs = sftp.CreateMultiDirectory(sfp);
                                }

                                if (sftp.IsConnected)
                                {
                                    msg = (createddirs.Count == 0) ? "Connected!" : "Connected!\r\nCreated folders:\r\n";
                                    for (int x = 0; x <= createddirs.Count - 1; x++)
                                    {
                                        msg += createddirs[x] + "\r\n";
                                    }
                                    msg += "\r\n\r\nPing results:\r\n " + SendPing(account.Host, 3);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        msg = e.Message;
                    }
                    break;
                default:
                    try
                    {
                        using (FTP ftpClient = new FTP(account))
                        {
                            if (ftpClient.ChangeDirectory(sfp, true))
                            {
                                msg = "Connected!\r\n\r\nPing results:\r\n" + SendPing(account.Host, 3);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        msg = e.Message;
                    }

                    break;
            }

            if (silent)
            {
                DebugHelper.WriteLine(string.Format("Tested {0} sub-folder path in {1}", sfp, account));
            }
            else
            {
                MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public static void TestFTPAccount(FTPAccount account)
        {
            string msg = string.Empty;
            string remotePath = account.GetSubFolderPath();
            List<string> directories = new List<string>();

            try
            {
                if (account.Protocol == FTPProtocol.FTP || account.Protocol == FTPProtocol.FTPS)
                {
                    using (FTP ftp = new FTP(account))
                    {
                        if (ftp.Connect())
                        {
                            if (!ftp.DirectoryExists(remotePath))
                            {
                                directories = ftp.CreateMultiDirectory(remotePath);
                            }

                            if (ftp.IsConnected)
                            {
                                if (directories.Count > 0)
                                {
                                    msg = "Connected!\r\nCreated folders:\r\n" + string.Join("\r\n", directories);
                                }
                                else
                                {
                                    msg = "Connected!";
                                }
                            }
                        }
                    }
                }
                else if (account.Protocol == FTPProtocol.SFTP)
                {
                    using (SFTP sftp = new SFTP(account))
                    {
                        if (sftp.Connect())
                        {
                            if (!sftp.DirectoryExists(remotePath))
                            {
                                directories = sftp.CreateMultiDirectory(remotePath);
                            }

                            if (sftp.IsConnected)
                            {
                                if (directories.Count > 0)
                                {
                                    msg = "Connected!\r\nCreated folders:\r\n" + string.Join("\r\n", directories);
                                }
                                else
                                {
                                    msg = "Connected!";
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                msg = e.Message;
            }

            MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Exemple #6
0
        public UploadResult UploadFile(Stream stream, string fileName)
        {
            FileUploader fileUploader = null;

            switch (Info.TaskSettings.FileDestination)
            {
                case FileDestination.Dropbox:
                    NameParser parser = new NameParser(NameParserType.URL);
                    string uploadPath = parser.Parse(Dropbox.TidyUploadPath(Program.UploadersConfig.DropboxUploadPath));
                    fileUploader = new Dropbox(Program.UploadersConfig.DropboxOAuthInfo, Program.UploadersConfig.DropboxAccountInfo)
                    {
                        UploadPath = uploadPath,
                        AutoCreateShareableLink = Program.UploadersConfig.DropboxAutoCreateShareableLink,
                        ShortURL = Program.UploadersConfig.DropboxShortURL
                    };
                    break;
                case FileDestination.GoogleDrive:
                    fileUploader = new GoogleDrive(Program.UploadersConfig.GoogleDriveOAuth2Info);
                    break;
                case FileDestination.RapidShare:
                    fileUploader = new RapidShare(Program.UploadersConfig.RapidShareUsername, Program.UploadersConfig.RapidSharePassword,
                        Program.UploadersConfig.RapidShareFolderID);
                    break;
                case FileDestination.SendSpace:
                    fileUploader = new SendSpace(ApiKeys.SendSpaceKey);
                    switch (Program.UploadersConfig.SendSpaceAccountType)
                    {
                        case AccountType.Anonymous:
                            SendSpaceManager.PrepareUploadInfo(ApiKeys.SendSpaceKey);
                            break;
                        case AccountType.User:
                            SendSpaceManager.PrepareUploadInfo(ApiKeys.SendSpaceKey, Program.UploadersConfig.SendSpaceUsername, Program.UploadersConfig.SendSpacePassword);
                            break;
                    }
                    break;
                case FileDestination.Minus:
                    fileUploader = new Minus(Program.UploadersConfig.MinusConfig, new OAuthInfo(ApiKeys.MinusConsumerKey, ApiKeys.MinusConsumerSecret));
                    break;
                case FileDestination.Box:
                    fileUploader = new Box(ApiKeys.BoxKey)
                    {
                        AuthToken = Program.UploadersConfig.BoxAuthToken,
                        FolderID = Program.UploadersConfig.BoxFolderID,
                        Share = Program.UploadersConfig.BoxShare
                    };
                    break;
                case FileDestination.Ge_tt:
                    if (Program.UploadersConfig.IsActive(FileDestination.Ge_tt))
                    {
                        fileUploader = new Ge_tt(ApiKeys.Ge_ttKey)
                        {
                            AccessToken = Program.UploadersConfig.Ge_ttLogin.AccessToken
                        };
                    }
                    break;
                case FileDestination.Localhostr:
                    fileUploader = new Hostr(Program.UploadersConfig.LocalhostrEmail, Program.UploadersConfig.LocalhostrPassword)
                    {
                        DirectURL = Program.UploadersConfig.LocalhostrDirectURL
                    };
                    break;
                case FileDestination.CustomFileUploader:
                    if (Program.UploadersConfig.CustomUploadersList.IsValidIndex(Program.UploadersConfig.CustomFileUploaderSelected))
                    {
                        fileUploader = new CustomFileUploader(Program.UploadersConfig.CustomUploadersList[Program.UploadersConfig.CustomFileUploaderSelected]);
                    }
                    break;
                case FileDestination.FTP:
                    int index = Info.TaskSettings.OverrideFTP ? Info.TaskSettings.FTPIndex.BetweenOrDefault(0, Program.UploadersConfig.FTPAccountList.Count - 1) : Program.UploadersConfig.GetFTPIndex(Info.DataType);

                    FTPAccount account = Program.UploadersConfig.FTPAccountList.ReturnIfValidIndex(index);

                    if (account != null)
                    {
                        if (account.Protocol == FTPProtocol.SFTP)
                        {
                            fileUploader = new SFTP(account);
                        }
                        else
                        {
                            fileUploader = new FTPUploader(account);
                        }
                    }
                    break;
                case FileDestination.SharedFolder:
                    int idLocalhost = Program.UploadersConfig.GetLocalhostIndex(Info.DataType);
                    if (Program.UploadersConfig.LocalhostAccountList.IsValidIndex(idLocalhost))
                    {
                        fileUploader = new SharedFolderUploader(Program.UploadersConfig.LocalhostAccountList[idLocalhost]);
                    }
                    break;
                case FileDestination.Email:
                    using (EmailForm emailForm = new EmailForm(Program.UploadersConfig.EmailRememberLastTo ? Program.UploadersConfig.EmailLastTo : string.Empty,
                        Program.UploadersConfig.EmailDefaultSubject, Program.UploadersConfig.EmailDefaultBody))
                    {
                        emailForm.Icon = ShareXResources.Icon;

                        if (emailForm.ShowDialog() == DialogResult.OK)
                        {
                            if (Program.UploadersConfig.EmailRememberLastTo)
                            {
                                Program.UploadersConfig.EmailLastTo = emailForm.ToEmail;
                            }

                            fileUploader = new Email
                            {
                                SmtpServer = Program.UploadersConfig.EmailSmtpServer,
                                SmtpPort = Program.UploadersConfig.EmailSmtpPort,
                                FromEmail = Program.UploadersConfig.EmailFrom,
                                Password = Program.UploadersConfig.EmailPassword,
                                ToEmail = emailForm.ToEmail,
                                Subject = emailForm.Subject,
                                Body = emailForm.Body
                            };
                        }
                        else
                        {
                            IsStopped = true;
                        }
                    }
                    break;
                case FileDestination.Jira:
                    fileUploader = new Jira(Program.UploadersConfig.JiraHost, Program.UploadersConfig.JiraOAuthInfo, Program.UploadersConfig.JiraIssuePrefix);
                    break;
                case FileDestination.Mega:
                    fileUploader = Program.UploadersConfig.MegaAnonymousLogin ? new Mega() : new Mega(Program.UploadersConfig.MegaAuthInfos, Program.UploadersConfig.MegaParentNodeId);
                    break;
            }

            if (fileUploader != null)
            {
                PrepareUploader(fileUploader);
                return fileUploader.Upload(stream, fileName);
            }

            return null;
        }
        public static void TestFTPAccount(FTPAccount account, bool silent)
        {
            string msg = string.Empty;
            string sfp = account.GetSubFolderPath();
            switch (account.Protocol)
            {
                case FTPProtocol.SFTP:
                    SFTP sftp = new SFTP(account);
                    if (!sftp.IsInstantiated)
                    {
                        msg = "An SFTP client couldn't be instantiated, not enough information.\nCould be a missing key file.";
                    }
                    else if (sftp.Connect())
                    {
                        List<string> createddirs = new List<string>();
                        if (!sftp.DirectoryExists(sfp))
                        {
                            createddirs = sftp.CreateMultipleDirectorys(FTPHelpers.GetPaths(sfp));
                        }
                        if (sftp.IsConnected)
                        {
                            msg = (createddirs.Count == 0) ? "Connected!" : "Conected!\nCreated folders;\n";
                            for (int x = 0; x <= createddirs.Count - 1; x++)
                            {
                                msg += createddirs[x] + "\n";
                            }
                            msg += " \n\nPing results:\n " + SendPing(account.Host, 3);
                            sftp.Disconnect();
                        }
                    }
                    break;
                default:
                    using (FTP ftpClient = new FTP(account))
                    {
                        try
                        {
                            //DateTime time = DateTime.Now;
                            ftpClient.Test(sfp);
                            msg = "Success!";
                        }
                        catch (Exception e)
                        {
                            if (e.Message.StartsWith("Could not change working directory to"))
                            {
                                try
                                {
                                    ftpClient.MakeMultiDirectory(sfp);
                                    ftpClient.Test(sfp);
                                    msg = "Success!\nAuto created folders: " + sfp;
                                }
                                catch (Exception e2)
                                {
                                    msg = e2.Message;
                                }
                            }
                            else
                            {
                                msg = e.Message;
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(msg))
                    {
                        string ping = SendPing(account.Host, 3);
                        if (!string.IsNullOrEmpty(ping))
                        {
                            msg += "\n\nPing results:\n" + ping;
                        }

                        if (silent)
                        {
                            // Engine.MyLogger.WriteLine(string.Format("Tested {0} sub-folder path in {1}", sfp, account.ToString()));
                        }
                        else
                        {
                            //MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    break;
            }
            if (silent)
            {
                DebugHelper.WriteLine(string.Format("Tested {0} sub-folder path in {1}", sfp, account.ToString()));
            }
            else
            {
                MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }