Class: Command::Backup
Constant Summary
collapse
- BACKUP_DIR_NAME =
"backup"
Instance Attribute Summary
Attributes inherited from CommandBase
#stream_io
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from CommandBase
#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids
Constructor Details
#initialize ⇒ Backup
Returns a new instance of Backup.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/command/backup.rb', line 18
def initialize
super("<target> [<target2> ...]")
@opt.separator <<-EOS
・指定した小説のバックアップを作成します。
・バックアップファイルはZIP圧縮され、小説保存フォルダ直下の#{BACKUP_DIR_NAME}フォルダに保存されます。
・バックアップ対象は、バックアップファイル以外の小説保存フォルダにあるファイル全てが対象です。
Examples:
narou backup 0
narou backup n9669bk
narou backup 0 1 musyoku
EOS
end
|
Class Method Details
.oneline_help ⇒ Object
14
15
16
|
# File 'lib/command/backup.rb', line 14
def self.oneline_help
"小説のバックアップを作成します"
end
|
Instance Method Details
#create_backup(data) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/command/backup.rb', line 39
def create_backup(data)
zipfilename = create_backup_filename(data)
novel_dir = Downloader.get_novel_data_dir_by_target(data["id"])
paths = novel_dir.glob("**/*").keep_if { |path|
relative_path = path_to_relative(novel_dir, path)
path.file? && relative_path.to_s.split("/", 2)[0] != BACKUP_DIR_NAME
}
backup_dir = novel_dir.join(BACKUP_DIR_NAME)
backup_dir.mkdir unless backup_dir.exist?
Zip.unicode_names = true unless Helper.os_windows?
Zip::File.open(backup_dir.join(zipfilename), Zip::File::CREATE) do |zip|
paths.each do |path|
relative_path = path_to_relative(novel_dir, path).to_s
zipped_filename =
if Helper.os_windows?
relative_path.encode(Encoding::Windows_31J, invalid: :replace, undef: :replace, replace: "_")
else
relative_path
end
zip.add(zipped_filename, path)
end
end
zipfilename
end
|
#create_backup_filename(data) ⇒ Object
33
34
35
36
37
|
# File 'lib/command/backup.rb', line 33
def create_backup_filename(data)
name = Helper.replace_filename_special_chars(data["title"])
name.slice!(-1) while name.bytesize > 180
"#{name}_#{Time.now.strftime("%Y%m%d%H%M%S")}.zip"
end
|
#execute(argv) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/command/backup.rb', line 68
def execute(argv)
super
display_help! if argv.empty?
tagname_to_ids(argv)
require "zip"
argv.each_with_index do |target, i|
Helper.print_horizontal_rule if i > 0
data = Downloader.get_data_by_target(target)
unless data
puts "#{target} は存在しません"
next
end
puts "ID:#{data["id"]} #{data["title"]}"
print "バックアップを作成しています"
Thread.new {
loop do
print "."
sleep(0.5)
end
}.tap { |th|
zipfilename = create_backup(data)
th.kill
puts
puts "#{zipfilename} を作成しました"
}
end
end
|
#path_to_relative(base, path) ⇒ Object
64
65
66
|
# File 'lib/command/backup.rb', line 64
def path_to_relative(base, path)
path.sub("#{base}/", "")
end
|