示例#1
0
 public Stream OpenWrite(string destFullPath)
 {
     // check folder existance
     CreateDirectory(PreparePath(FileTransferHelpers.SplitByLastPathPart(destFullPath).Item1));
     // upload
     return(client.OpenWrite(PreparePath(destFullPath)));
 }
        public bool SendFile(Stream localFile, string destFullPath, bool closeInputStream)
        {
            // try to upload file
            for (int i = 0; i < Details.RetryCount; i++)
            {
                // try to open
                if (!Open())
                {
                    return(false);
                }
                try
                {
                    CreateDirectory(FileTransferHelpers.SplitByLastPathPart(destFullPath).Item1);

                    client.UploadFile(localFile, PreparePath(destFullPath), true);
                    _setStatus(true);
                    return(true);
                }
                catch (Exception ex)
                {
                    _setStatus(ex);
                    // disconnect on error
                    Close();
                }
                finally
                {
                    if (closeInputStream)
                    {
                        localFile.Close();
                    }
                }
            }
            return(false);
        }
示例#3
0
        public FileTransferInfo GetFile(string file, string outputDirectory, bool deleteOnSuccess)
        {
            outputDirectory = outputDirectory.Replace('\\', '/');
            if (!outputDirectory.EndsWith("/"))
            {
                outputDirectory += "/";
            }
            FileTransferHelpers.CreateDirectory(outputDirectory);

            // download files
            var f = GetFileStream(file);

            string newFile = System.IO.Path.Combine(outputDirectory, System.IO.Path.GetFileName(f.FileName));

            FileTransferHelpers.DeleteFile(newFile);

            try
            {
                using (var output = new FileStream(newFile, FileMode.Create, FileAccess.Write, FileShare.Delete, FileServiceConnectionInfo.DefaultWriteBufferSize))
                {
                    f.FileStream.CopyTo(output, FileServiceConnectionInfo.DefaultWriteBufferSize >> 2);
                }

                // check if we must remove file
                if (deleteOnSuccess)
                {
                    FileTransferHelpers.DeleteFile(f.FileName);
                }

                _setStatus(true);
            }
            catch (Exception ex)
            {
                _setStatus(ex);
                FileTransferHelpers.DeleteFile(newFile);
                newFile = null;
            }
            finally
            {
                f.FileStream.Close();
            }

            // check if file was downloaded
            if (newFile != null)
            {
                var info = new System.IO.FileInfo(newFile);
                if (info.Exists)
                {
                    return(new FileTransferInfo(newFile, info.Length, info.CreationTime, info.LastWriteTime));
                }
            }
            return(null);
        }
示例#4
0
        public bool SendFile(Stream localFile, string destFullPath, bool closeInputStream)
        {
            // try to upload file
            for (int i = 0; i < Details.RetryCount; i++)
            {
                // try to open
                if (!Open())
                {
                    return(false);
                }
                try
                {
                    // check folder existance
                    CreateDirectory(PreparePath(FileTransferHelpers.SplitByLastPathPart(destFullPath).Item1));

                    // upload
                    using (Stream ostream = client.OpenWrite(PreparePath(destFullPath)))
                    {
                        using (var file = localFile)
                        {
                            file.CopyTo(ostream, FileServiceConnectionInfo.DefaultWriteBufferSize >> 2);
                        }
                    }
                    _setStatus(true);
                    return(true);
                }
                catch (Exception ex)
                {
                    _setStatus(ex);
                    // disconnect on error
                    Close();
                }
                finally
                {
                    if (closeInputStream)
                    {
                        localFile.Close();
                    }
                }
            }
            return(false);
        }
示例#5
0
        public IEnumerable <FileTransferInfo> GetFiles(string folder, string fileMask, bool recursive, string outputDirectory, bool deleteOnSuccess)
        {
            outputDirectory = outputDirectory.Replace('\\', '/');
            if (!outputDirectory.EndsWith("/"))
            {
                outputDirectory += "/";
            }
            FileTransferHelpers.CreateDirectory(outputDirectory);

            // download files
            foreach (var f in GetFileStreams(folder, fileMask, recursive))
            {
                string newFile = null;

                // download file
                try
                {
                    newFile = System.IO.Path.Combine(outputDirectory, System.IO.Path.GetFileName(f.FileName));
                    FileTransferHelpers.DeleteFile(newFile);

                    using (var file = new FileStream(newFile, FileMode.Create, FileAccess.Write, FileShare.Delete, FileServiceConnectionInfo.DefaultWriteBufferSize))
                    {
                        f.FileStream.CopyTo(file, FileServiceConnectionInfo.DefaultWriteBufferSize >> 2);
                    }

                    f.FileStream.Close();
                    f.FileStream = null;

                    // check if we must remove file
                    if (deleteOnSuccess && System.IO.File.Exists(newFile))
                    {
                        client.DeleteFile(f.FileName);
                    }

                    _setStatus(true);
                    break;
                }
                catch (Exception ex)
                {
                    _setStatus(ex);
                    Close();
                    FileTransferHelpers.DeleteFile(newFile);
                    newFile = null;
                    // delay in case of network error
                    System.Threading.Thread.Sleep(Details.RetryWaitMs);
                    // try to reopen
                    Open();
                }
                finally
                {
                    if (f.FileStream != null)
                    {
                        f.FileStream.Close();
                    }
                }

                // check if file was downloaded
                if (newFile != null)
                {
                    var info = new System.IO.FileInfo(newFile);
                    if (info.Exists)
                    {
                        yield return(new FileTransferInfo(newFile, info.Length, info.CreationTime, info.LastWriteTime));
                    }
                }
            }
        }
示例#6
0
 public string GetFileAsText(string file)
 {
     using (var reader = new StreamReader(GetFileStream(file).FileStream, FileTransferHelpers.TryGetEncoding(Details.Get("encoding", "ISO-8859-1")), true))
         return(reader.ReadToEnd());
 }
示例#7
0
        public IEnumerable <FileTransferInfo> ListFiles(string folder, string fileMask, bool recursive)
        {
            var pattern = String.IsNullOrEmpty(fileMask) ? null : new System.Text.RegularExpressions.Regex(FileTransferHelpers.WildcardToRegex(fileMask), System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);

            return(_listFiles(folder, pattern, recursive));
        }
示例#8
0
        public IEnumerable <StreamTransferInfo> GetFileStreams(string folder, string fileMask, bool recursive)
        {
            var pattern = String.IsNullOrEmpty(fileMask) ? null : new System.Text.RegularExpressions.Regex(FileTransferHelpers.WildcardToRegex(fileMask), System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);

            // get files
            List <FtpListItem> files = null;

            for (int i = 0; i < Details.RetryCount; i++)
            {
                // try to open
                if (!Open())
                {
                    continue;
                }
                try
                {
                    // get all files to download, but limit to 2MM
                    files = _listFiles(folder, pattern, recursive).Take(2000000).ToList();
                    _setStatus(true);
                    break;
                }
                catch (Exception ex)
                {
                    _setStatus(ex);
                    // disconnect on error
                    Close();
                    // delay in case of network error
                    System.Threading.Thread.Sleep(Details.RetryWaitMs);
                }
            }

            // sanity check
            if (files == null)
            {
                yield break;
            }

            _setStatus(true);
            // download files
            foreach (var f in files)
            {
                StreamTransferInfo file = null;
                try
                {
                    file = new StreamTransferInfo
                    {
                        FileName   = f.FullName,
                        FileStream = client.OpenRead(f.FullName)
                    };
                }
                catch (Exception ex)
                {
                    LastError = ex.Message;
                    // skip file
                }
                if (file != null)
                {
                    yield return(file);
                }
            }
        }