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 Method Summary collapse

Constructor Details

#initialize(host, **opts) ⇒ FTP

Initializes a new bucket

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.



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

def initialize(host, **opts)
  opts = opts.dup
  opts.keys.each do |key|
    val = opts.delete(key)
    opts[key.to_sym] = val unless val.nil?
  end
  super(**opts)

  prefix  = opts.delete(:prefix)
  @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 Method Details

#closeObject

Closes the underlying connection



95
96
97
# File 'lib/bfs/bucket/ftp.rb', line 95

def close
  @client.close
end

#create(path, **opts, &block) ⇒ Object

Creates a new file and opens it for writing



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bfs/bucket/ftp.rb', line 58

def create(path, **opts, &block)
  path = norm_path(path)
  enc  = opts.delete(:encoding) || @encoding
  temp = BFS::TempWriter.new(path, encoding: enc) do |t|
    mkdir_p File.dirname(path)
    @client.put(t, path)
  end
  return temp unless block

  begin
    yield temp
  ensure
    temp.close
  end
end

#info(path, **_opts) ⇒ Object

Info returns the object info



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

def info(path, **_opts)
  path = norm_path(path)
  BFS::FileInfo.new(path, @client.size(path), @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



38
39
40
41
42
43
44
45
46
47
# File 'lib/bfs/bucket/ftp.rb', line 38

def ls(pattern = '**/*', **_opts)
  dir = pattern[%r{^[^\*\?\{\}\[\]]+/}]
  dir&.chomp!('/')

  Enumerator.new do |y|
    glob(dir) do |path|
      y << path if File.fnmatch?(pattern, path, File::FNM_PATHNAME)
    end
  end
end

#open(path, **opts, &block) ⇒ Object

Opens an existing file for reading



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bfs/bucket/ftp.rb', line 75

def open(path, **opts, &block)
  path = norm_path(path)
  enc  = opts.delete(:encoding) || @encoding
  temp = Tempfile.new(File.basename(path), encoding: enc)
  temp.close

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

#rm(path, **_opts) ⇒ Object

Deletes a file.



88
89
90
91
92
# File 'lib/bfs/bucket/ftp.rb', line 88

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