Class: Pione::Util::FTPServer

Inherits:
Object
  • Object
show all
Defined in:
lib/pione/util/ftp-server.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.auth_infoObject

Returns the value of attribute auth_info.



289
290
291
# File 'lib/pione/util/ftp-server.rb', line 289

def auth_info
  @auth_info
end

.fsObject

Returns the value of attribute fs.



290
291
292
# File 'lib/pione/util/ftp-server.rb', line 290

def fs
  @fs
end

.portObject

Returns the value of attribute port.



291
292
293
# File 'lib/pione/util/ftp-server.rb', line 291

def port
  @port
end

.threadObject (readonly)

Returns the value of attribute thread.



292
293
294
# File 'lib/pione/util/ftp-server.rb', line 292

def thread
  @thread
end

Class Method Details

.make_location(path) ⇒ Object



310
311
312
# File 'lib/pione/util/ftp-server.rb', line 310

def make_location(path)
  Location["ftp://%s:%s@localhost:%i%s" % [@auth_info.user, @auth_info.password, @port, path]]
end

.start(fs) ⇒ Object

Start FTP server.



295
296
297
298
299
300
301
302
# File 'lib/pione/util/ftp-server.rb', line 295

def start(fs)
  @fs = fs
  @thread = Thread.new do
    EventMachine.run do
      @sig = EventMachine.start_server("0.0.0.0", @port, EM::FTPD::Server, self)
    end
  end
end

.stopObject



304
305
306
307
308
# File 'lib/pione/util/ftp-server.rb', line 304

def stop
  EventMachine.stop_event_loop
  EventMachine.reactor_thread.join
  @thread.kill if @thread
end

Instance Method Details

#authenticate(user, password) {|auth_info.user == user && auth_info.password == password| ... } ⇒ Object

Authenticate the user.

Yields:



342
343
344
# File 'lib/pione/util/ftp-server.rb', line 342

def authenticate(user, password, &b)
  yield auth_info.user == user && auth_info.password == password
end

#bytes(path, &b) ⇒ Object

Get byte size of the path.



347
348
349
350
351
352
353
354
355
356
# File 'lib/pione/util/ftp-server.rb', line 347

def bytes(path, &b)
  path = Pathname.new(path).cleanpath
  if fs.file?(path)
    yield fs.get_size(path)
  elsif fs.directory?(path)
    yield -1
  else
    yield false
  end
end

#change_dir(path) {|fs.directory?(path)| ... } ⇒ Object

Change directory.

Yields:

  • (fs.directory?(path))


318
319
320
321
# File 'lib/pione/util/ftp-server.rb', line 318

def change_dir(path, &b)
  path = Pathname.new(path).cleanpath
  yield fs.directory?(path)
end

#delete_dir(path, &b) ⇒ Object

Delete the directory.



425
426
427
428
429
430
431
432
433
# File 'lib/pione/util/ftp-server.rb', line 425

def delete_dir(path, &b)
  path = Pathname.new(path).cleanpath
  if fs.directory?(path) and fs.entries(path).empty?
    fs.rmdir(path)
    yield true
  else
    yield false
  end
end

#delete_file(path, &b) ⇒ Object

Delete file of the path.



382
383
384
385
386
387
388
389
390
391
392
# File 'lib/pione/util/ftp-server.rb', line 382

def delete_file(path, &b)
  path = Pathname.new(path).cleanpath
  dir = path.dirname
  filename = path.basename
  if fs.directory?(dir) and fs.entries(dir).include?(filename)
    fs.delete_file(path)
    yield true
  else
    yield false
  end
end

#dir_contents(path, &b) ⇒ Object

Return entries of the directory.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/pione/util/ftp-server.rb', line 324

def dir_contents(path, &b)
  path = Pathname.new(path).cleanpath
  if fs.directory?(path)
    entries = fs.entries(path).map do |entry|
      entry_path = path + entry
      if fs.directory?(entry_path)
        dir_item(entry)
      else
        file_item(entry, fs.get_size(entry_path))
      end
    end
    yield entries
  else
    yield Set.new
  end
end

#get_file(path, &block) ⇒ Object

Get file content of the path.



359
360
361
362
363
364
365
366
# File 'lib/pione/util/ftp-server.rb', line 359

def get_file(path, &block)
  path = Pathname.new(path).cleanpath
  if fs.file?(path)
    yield fs.get_file(path)
  else
    yield false
  end
end

#make_dir(path, &b) ⇒ Object

Make the directory.



413
414
415
416
417
418
419
420
421
422
# File 'lib/pione/util/ftp-server.rb', line 413

def make_dir(path, &b)
  path = Pathname.new(path).cleanpath
  dir = path.dirname
  if fs.exist?(path) or not(fs.directory?(dir))
    yield false
  else
    fs.mkdir(path)
    yield true
  end
end

#mtime(path) ⇒ Object

Return the mtime.



436
437
438
439
440
441
442
443
# File 'lib/pione/util/ftp-server.rb', line 436

def mtime(path)
  path = Pathname.new(path).cleanpath
  if mtime = fs.get_mtime(path)
    yield mtime
  else
    yield false
  end
end

#put_file(path, data, &b) ⇒ Object

Put data to the path.



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/pione/util/ftp-server.rb', line 369

def put_file(path, data, &b)
  path = Pathname.new(path).cleanpath
  dir = path.dirname
  filename = path.basename
  if fs.directory?(dir) and filename
    fs.put_file(path, data)
    yield data.size
  else
    yield false
  end
end

#rename(from_path, to_path, &b) ⇒ Object



445
446
447
448
449
450
451
452
453
454
# File 'lib/pione/util/ftp-server.rb', line 445

def rename(from_path, to_path, &b)
  from_path = Pathname.new(from_path).cleanpath
  to_path = Pathname.new(to_path).cleanpath
  if fs.file?(from_path) and fs.directory?(to_path.dirname)
    fs.mv(from_path, to_path)
    yield true
  else
    yield false
  end
end

#rename_file(from, to, &b) ⇒ Object

Rename the file.



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/pione/util/ftp-server.rb', line 395

def rename_file(from, to, &b)
  from_path = Pathname.new(from).cleanpath
  from_dir = from_path.dirname
  from_filename = from_path.basename
  to_path = Pathname.new(to).cleanpath
  to_dir = to_path.dirname
  to_filename = to_path.basename
  if fs.file?(from_path) && fs.directory?(to_dir)
    data = fs.get_file(from_path)
    fs.delete_file(from_path)
    fs.put_file(to_path, data)
    yield true
  else
    yield false
  end
end