1
0

file_store.rb 957 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require 'fileutils'
  2. require 'find'
  3. module Docs
  4. class FileStore < AbstractStore
  5. private
  6. def read_file(path)
  7. File.read(path)
  8. end
  9. def create_file(path, value)
  10. FileUtils.mkpath File.dirname(path)
  11. if value.is_a? Tempfile
  12. FileUtils.move(value, path)
  13. else
  14. File.write(path, value)
  15. end
  16. end
  17. alias_method :update_file, :create_file
  18. def delete_file(path)
  19. if File.directory?(path)
  20. FileUtils.rmtree(path, secure: true)
  21. else
  22. FileUtils.rm(path)
  23. end
  24. end
  25. def file_exist?(path)
  26. File.exist?(path)
  27. end
  28. def file_mtime(path)
  29. File.mtime(path)
  30. end
  31. def file_size(path)
  32. File.size(path)
  33. end
  34. def list_files(path)
  35. Find.find path do |file|
  36. next if file == path
  37. Find.prune if File.basename(file)[0] == '.'
  38. yield file
  39. Find.prune unless File.exist?(file)
  40. end
  41. end
  42. end
  43. end