Skip to content

Commit

Permalink
Added methods for working with files (#241)
Browse files Browse the repository at this point in the history
* Added methods for working with files
copy file
move file
get file size

* Added tests for new methods
* update CHANGELOG.md
  • Loading branch information
l8556 authored Aug 12, 2022
1 parent ab54d1f commit d87b1a0
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## master (unreleased)

### New Features

* New method `OnlyofficeFileHelper.copy_file`
* New method `OnlyofficeFileHelper.move_file`
* New method `OnlyofficeFileHelper.file_size`

## 1.0.0 (2022-08-06)

### New Features
Expand Down
2 changes: 2 additions & 0 deletions lib/onlyoffice_file_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'find'
require 'onlyoffice_file_helper/create_methods'
require 'onlyoffice_file_helper/directory_methods'
require 'onlyoffice_file_helper/file_methods'
require 'onlyoffice_file_helper/read_methods'
require 'onlyoffice_file_helper/version'
require 'onlyoffice_file_helper/linux_helper'
Expand All @@ -20,6 +21,7 @@ class FileHelper
extend CreateMethods
extend DirectoryMethods
extend ReadMethods
extend FileMethods

class << self
# Return name of file from full path
Expand Down
31 changes: 31 additions & 0 deletions lib/onlyoffice_file_helper/file_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module OnlyofficeFileHelper
# Methods used to work with files
module FileMethods
# Copies a file
# @param [String] file_path path to file to be copied
# @param [String] destination path to where to copy file
def copy_file(file_path, destination)
FileUtils.mkdir_p(destination) unless File.directory?(destination)
FileUtils.copy(file_path, destination)
end

# Moves a file
# @param [String] file_path path to file to be moved
# @param [String] destination path to where to move file
def move_file(file_path, destination)
FileUtils.mkdir_p(destination) unless File.directory?(destination)
FileUtils.move(file_path, destination)
end

# Get file size in bytes
# @param [String] file_path path to file
# @return [Integer] size of file in bytes
def file_size(file_path)
size = File.size?(file_path)
size = 0 if size.nil?
size
end
end
end
24 changes: 24 additions & 0 deletions spec/onlyoffice_file_helper/file_helper_copy_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe OnlyofficeFileHelper::FileHelper, '.copy_file' do
let(:file_path) { Tempfile.new('copy').path.to_s }
let(:destination) { "/tmp/#{Random.hex(12)}/" }

it 'copied directory created' do
described_class.copy_file(file_path, destination)
expect(File).to be_directory(destination)
end

it 'if copied folder already exists' do
described_class.create_folder(destination)
described_class.copy_file(file_path, destination)
expect(File).to be_directory(destination)
end

it 'file exists in the new directory' do
described_class.copy_file(file_path, destination)
expect(File).to exist(destination)
end
end
20 changes: 20 additions & 0 deletions spec/onlyoffice_file_helper/file_helper_get_file_size_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe OnlyofficeFileHelper::FileHelper, '.get_file_size' do
let(:file_path) { "/tmp/file_size#{Random.hex(12)}" }

it 'get file size' do
described_class.create_file_with_size(file_path: file_path, size: '15K')
expect(described_class.file_size(file_path)).to be(15_360)
end

it 'get file size if file size 0' do
expect(described_class.file_size(Tempfile.new('size').path.to_s)).to be(0)
end

it 'get file size if nonexistent file' do
expect(described_class.file_size("/tmp/nonexistent_file#{Random.hex(12)}")).to be(0)
end
end
29 changes: 29 additions & 0 deletions spec/onlyoffice_file_helper/file_helper_move_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe OnlyofficeFileHelper::FileHelper, '.move_file' do
let(:file_path) { Tempfile.new('move').path.to_s }
let(:destination) { "/tmp/#{Random.hex(12)}/" }

it 'if moved folder already exists' do
described_class.create_folder(destination)
described_class.move_file(file_path, destination)
expect(File).to be_directory(destination)
end

it 'moved directory created' do
described_class.move_file(file_path, destination)
expect(File).to be_directory(destination)
end

it 'file is moved from the source directory' do
described_class.move_file(file_path, destination)
expect(File).not_to exist(file_path)
end

it 'file exists in the new directory' do
described_class.move_file(file_path, destination)
expect(File).to exist(destination)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
end
require 'bundler/setup'
require 'onlyoffice_file_helper'
require 'tempfile'
require 'securerandom'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down

0 comments on commit d87b1a0

Please sign in to comment.