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)


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/onlyoffice_file_helper.rb', line 58

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



85
86
87
88
89
# File 'lib/onlyoffice_file_helper.rb', line 85

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



31
32
33
34
35
# File 'lib/onlyoffice_file_helper.rb', line 31

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)


76
77
78
79
80
# File 'lib/onlyoffice_file_helper.rb', line 76

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



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/onlyoffice_file_helper.rb', line 42

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