Class: BFS::Bucket::FTP

Inherits:
Abstract
  • Object
show all
Defined in:
lib/bfs/bucket/ftp.rb

Overview

FTP buckets are operating on ftp servers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, prefix: nil, **opts) ⇒ FTP

Initializes a new bucket

Parameters:

  • host (String)

    the host name

  • opts (Hash)

    options

Options Hash (**opts):

  • :port (Integer)

    custom port. Default: 21.

  • :ssl (Boolean)

    will attempt to use SSL.

  • :username (String)

    user name for login.

  • :password (String)

    password for login.

  • :passive (String)

    connect in passive mode. Default: true.

  • :prefix (String)

    optional prefix.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bfs/bucket/ftp.rb', line 19

def initialize(host, prefix: nil, **opts)
  super(**opts)

  @client = Net::FTP.new(host, **opts)
  @client.binary = true

  if prefix # rubocop:disable Style/GuardClause
    prefix = norm_path(prefix)
    mkdir_p(prefix)
    @client.chdir(prefix)
  end
end

Instance Attribute Details

#permObject (readonly)

Returns the value of attribute perm.



8
9
10
# File 'lib/bfs/bucket/ftp.rb', line 8

def perm
  @perm
end

Instance Method Details

#closeObject

Closes the underlying connection



87
88
89
# File 'lib/bfs/bucket/ftp.rb', line 87

def close
  @client.close
end

#create(path, encoding: self.encoding, perm: self.perm, **_opts, &block) ⇒ Object

Creates a new file and opens it for writing



59
60
61
62
63
64
65
# File 'lib/bfs/bucket/ftp.rb', line 59

def create(path, encoding: self.encoding, perm: self.perm, **_opts, &block)
  path = norm_path(path)
  BFS::Writer.new(path, encoding: encoding, perm: perm) do |t|
    mkdir_p File.dirname(path)
    @client.put(t, path)
  end.perform(&block)
end

#glob(pattern = '**/*', **_opts) ⇒ Object

Iterates over the contents of a bucket using a glob pattern



42
43
44
45
46
47
48
# File 'lib/bfs/bucket/ftp.rb', line 42

def glob(pattern = '**/*', **_opts)
  Enumerator.new do |y|
    walk(pattern) do |path, entry|
      y << file_info(path, entry) if File.fnmatch?(pattern, path, File::FNM_PATHNAME)
    end
  end
end

#info(path, **_opts) ⇒ Object

Info returns the object info



51
52
53
54
55
56
# File 'lib/bfs/bucket/ftp.rb', line 51

def info(path, **_opts)
  path = norm_path(path)
  BFS::FileInfo.new(path: path, size: @client.size(path), mtime: @client.mtime(path))
rescue Net::FTPPermError
  raise BFS::FileNotFound, path
end

#ls(pattern = '**/*', **_opts) ⇒ Object

Lists the contents of a bucket using a glob pattern



33
34
35
36
37
38
39
# File 'lib/bfs/bucket/ftp.rb', line 33

def ls(pattern = '**/*', **_opts)
  Enumerator.new do |y|
    walk(pattern) do |path, _|
      y << path if File.fnmatch?(pattern, path, File::FNM_PATHNAME)
    end
  end
end

#open(path, encoding: self.encoding, perm: self.perm, tempdir: nil, **_opts, &block) ⇒ Object

Opens an existing file for reading



68
69
70
71
72
73
74
75
76
77
# File 'lib/bfs/bucket/ftp.rb', line 68

def open(path, encoding: self.encoding, perm: self.perm, tempdir: nil, **_opts, &block)
  path = norm_path(path)
  temp = Tempfile.new(File.basename(path), tempdir, encoding: encoding, perm: perm)
  temp.close

  @client.get(path, temp.path)
  File.open(temp.path, encoding: encoding, &block)
rescue Net::FTPPermError
  raise BFS::FileNotFound, path
end

#rm(path, **_opts) ⇒ Object

Deletes a file.



80
81
82
83
84
# File 'lib/bfs/bucket/ftp.rb', line 80

def rm(path, **_opts)
  path = norm_path(path)
  @client.delete(path)
rescue Net::FTPPermError # rubocop:disable Lint/SuppressedException
end