Example #1
0
        public void Put(string remotename, System.IO.Stream input)
        {
            string remotePath = remotename;
            long   streamLen  = -1;

            try
            {
                var ftpClient = CreateClient();

                try
                {
                    streamLen = input.Length;
                }
                // ReSharper disable once EmptyGeneralCatchClause
                catch
                {
                }

                // Get the remote path
                remotePath = "";

                if (!string.IsNullOrEmpty(remotename))
                {
                    // Append the filename
                    remotePath += remotename;
                }

                using (var outputStream = ftpClient.OpenWrite(remotePath))
                {
                    try
                    {
                        CoreUtility.CopyStream(input, outputStream, true, _copybuffer);
                    }
                    finally
                    {
                        outputStream.Close();
                    }
                }


                if (_listVerify)
                {
                    var fileEntries = List(remotename, true);

                    foreach (var fileEntry in fileEntries)
                    {
                        if (fileEntry.Name.Equals(remotename) || fileEntry.Name.EndsWith("/" + remotename) || fileEntry.Name.EndsWith("\\" + remotename))
                        {
                            if (fileEntry.Size < 0 || streamLen < 0 || fileEntry.Size == streamLen)
                            {
                                return;
                            }

                            throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, fileEntry.Size, streamLen));
                        }
                    }

                    throw new UserInformationException(Strings.ListVerifyFailure(remotename, fileEntries.Select(n => n.Name)));
                }
            }
            catch (FtpCommandException ex)
            {
                if (ex.Message == "Directory not found.")
                {
                    throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex);
                }

                throw;
            }
        }
Example #2
0
        private List <IFileEntry> List(string filename, bool stripFile)
        {
            var    list       = new List <IFileEntry>();
            string remotePath = filename;

            try
            {
                var ftpClient = CreateClient();

                // Get the remote path
                var url = new Uri(this._url);
                remotePath = "/" + (url.AbsolutePath.EndsWith("/") ? url.AbsolutePath.Substring(0, url.AbsolutePath.Length - 1) : url.AbsolutePath);

                if (!string.IsNullOrEmpty(filename))
                {
                    if (!stripFile)
                    {
                        // Append the filename
                        remotePath += filename;
                    }
                    else if (filename.Contains("/"))
                    {
                        remotePath += filename.Substring(0, filename.LastIndexOf("/", StringComparison.Ordinal));
                    }
                    // else: stripping the filename in this case ignoring it
                }

                foreach (FtpListItem item in ftpClient.GetListing(remotePath, FtpListOption.Modify | FtpListOption.Size | FtpListOption.DerefLinks))
                {
                    switch (item.Type)
                    {
                    case FtpFileSystemObjectType.Directory:
                    {
                        if (item.Name == "." || item.Name == "..")
                        {
                            continue;
                        }

                        list.Add(new FileEntry(item.Name, -1, new DateTime(), item.Modified)
                            {
                                IsFolder = true,
                            });

                        break;
                    }

                    case FtpFileSystemObjectType.File:
                    {
                        list.Add(new FileEntry(item.Name, item.Size, new DateTime(), item.Modified));

                        break;
                    }

                    case FtpFileSystemObjectType.Link:
                    {
                        if (item.Name == "." || item.Name == "..")
                        {
                            continue;
                        }

                        if (item.LinkObject != null)
                        {
                            switch (item.LinkObject.Type)
                            {
                            case FtpFileSystemObjectType.Directory:
                            {
                                if (item.Name == "." || item.Name == "..")
                                {
                                    continue;
                                }

                                list.Add(new FileEntry(item.Name, -1, new DateTime(), item.Modified)
                                        {
                                            IsFolder = true,
                                        });

                                break;
                            }

                            case FtpFileSystemObjectType.File:
                            {
                                list.Add(new FileEntry(item.Name, item.Size, new DateTime(), item.Modified));

                                break;
                            }
                            }
                        }
                        break;
                    }
                    }
                }
            }//         Message    "Directory not found."    string
            catch (FtpCommandException ex)
            {
                if (ex.Message == "Directory not found.")
                {
                    throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex);
                }

                throw;
            }

            return(list);
        }