Class: OnlyofficeFileHelper::FileHelper

Inherits:
Object
  • Object
show all
Extended by:
CreateMethods, DirectoryMethods, ReadMethods
Defined in:
lib/onlyoffice_file_helper.rb

Overview

Stuff for working with Files

Class Method Summary collapse

Methods included from CreateMethods

create_file_with_content, create_file_with_size, create_folder

Methods included from DirectoryMethods

delete_directory, directory_hash, list_file_in_directory

Methods included from ReadMethods

read_array_from_file, read_file_to_string, read_specific_line

Class Method Details

.extract_to_folder(path_to_archive, path_to_extract = path_to_archive.chomp(File.basename(path_to_archive))) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/onlyoffice_file_helper.rb', line 45

def extract_to_folder(path_to_archive,
                      path_to_extract = path_to_archive.chomp(File.basename(path_to_archive)))
  raise 'File not found: ' + path_to_archive.to_s unless wait_file_to_download(path_to_archive)

  path_to_extract += '/' unless path_to_extract[-1] == '/'
  path_to_file = path_to_extract + File.basename(path_to_archive)
  Zip::File.open(path_to_file) do |zip_file|
    zip_file.each do |file|
      file_path = File.join(path_to_extract, file.name)
      create_folder(File.dirname(file_path))
      zip_file.extract(file, file_path)
    end
  end
end

.file_line_count(file_name) ⇒ Fixnum

Get line count in file

Parameters:

  • file_name (String)

    name of file

Returns:

  • (Fixnum)

    count of lines in file



69
70
71
72
73
# File 'lib/onlyoffice_file_helper.rb', line 69

def file_line_count(file_name)
  line_count = `wc -l < #{file_name}`.to_i
  OnlyofficeLoggerHelper.log("Count of lines in '#{file_name}' is #{line_count}")
  line_count
end

.get_filename(file_path, keep_extension = true) ⇒ Sting

Return name of file from full path

Parameters:

  • keep_extension (true, false) (defaults to: true)

    keep extension in result?

Returns:

  • (Sting)

    name of file, with extension or not



26
27
28
29
30
# File 'lib/onlyoffice_file_helper.rb', line 26

def get_filename(file_path, keep_extension = true)
  name = Pathname.new(file_path).basename
  name = File.basename(name, File.extname(name)) unless keep_extension
  name.to_s
end

.output_string_to_file(string, file_name) ⇒ Object



60
61
62
63
64
# File 'lib/onlyoffice_file_helper.rb', line 60

def output_string_to_file(string, file_name)
  File.open(file_name, 'a+') do |f1|
    f1.write(string)
  end
end

.wait_file_to_download(path, timeout = 300) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/onlyoffice_file_helper.rb', line 32

def wait_file_to_download(path, timeout = 300)
  timer = 0
  OnlyofficeLoggerHelper.log("Start waiting to download file: #{path}")
  until File.exist?(path) && !File.exist?(path + '.part')
    OnlyofficeLoggerHelper.log("Waiting for #{timer} seconds from #{timeout}")
    sleep 1
    timer += 1
    raise "Timeout #{timeout} for downloading file #{path} is exceed" if timer > timeout
  end
  sleep 1
  timer <= timeout
end