Class: OnlyofficeFileHelper::FileHelper

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

Overview

Stuff for working with Files

Constant Summary

Constants included from DirectoryMethods

DirectoryMethods::LINUX_SPECIAL_DIRS

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

Methods included from FileMethods

copy_file, file_size, move_file

Class Method Details

.extract_to_folder(path_to_archive) ⇒ Void

Extract archive to folder

Parameters:

  • path_to_archive (String)

    path of file

Returns:

  • (Void)


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/onlyoffice_file_helper.rb', line 55

def extract_to_folder(path_to_archive)
  wait_file_to_download(path_to_archive)

  path_to_extract = path_to_archive.chomp(File.basename(path_to_archive))
  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



82
83
84
85
86
# File 'lib/onlyoffice_file_helper.rb', line 82

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

.filename_from_path(file_path, keep_extension: true) ⇒ Sting

Return name of file from full path

Parameters:

  • file_path (String)

    to get name

  • keep_extension (Boolean) (defaults to: true)

    keep extension in result?

Returns:

  • (Sting)

    name of file, with extension or not



28
29
30
31
32
# File 'lib/onlyoffice_file_helper.rb', line 28

def filename_from_path(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) ⇒ Void

Save string to file

Parameters:

  • string (String)

    string to save

  • file_name (String)

    file to save

Returns:

  • (Void)


73
74
75
76
77
# File 'lib/onlyoffice_file_helper.rb', line 73

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) ⇒ True

Wait for downloading file

Parameters:

  • path (String)

    path to waiting download

  • timeout (Integer) (defaults to: 300)

    timeout to wait

Returns:

  • (True)

    always successful, if not - raising Exception

Raises:

  • (StandardError)

    exception if file not downloaded during timeout



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/onlyoffice_file_helper.rb', line 39

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
  true
end