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 "\n \u30FB\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u30D0\u30C3\u30AF\u30A2\u30C3\u30D7\u3092\u4F5C\u6210\u3057\u307E\u3059\u3002\n \u30FB\u30D0\u30C3\u30AF\u30A2\u30C3\u30D7\u30D5\u30A1\u30A4\u30EB\u306FZIP\u5727\u7E2E\u3055\u308C\u3001\u5C0F\u8AAC\u4FDD\u5B58\u30D5\u30A9\u30EB\u30C0\u76F4\u4E0B\u306E\#{BACKUP_DIR_NAME}\u30D5\u30A9\u30EB\u30C0\u306B\u4FDD\u5B58\u3055\u308C\u307E\u3059\u3002\n \u30FB\u30D0\u30C3\u30AF\u30A2\u30C3\u30D7\u5BFE\u8C61\u306F\u3001\u30D0\u30C3\u30AF\u30A2\u30C3\u30D7\u30D5\u30A1\u30A4\u30EB\u4EE5\u5916\u306E\u5C0F\u8AAC\u4FDD\u5B58\u30D5\u30A9\u30EB\u30C0\u306B\u3042\u308B\u30D5\u30A1\u30A4\u30EB\u5168\u3066\u304C\u5BFE\u8C61\u3067\u3059\u3002\n\n Examples:\nnarou backup 0\nnarou backup n9669bk\nnarou backup 0 1 musyoku\n EOS\nend\n"
|
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/command/backup.rb', line 37
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
|
# File 'lib/command/backup.rb', line 33
def create_backup_filename(data)
Helper.replace_filename_special_chars(data["title"]) + "_" + Time.now.strftime("%Y%m%d%H%M%S") + ".zip"
end
|
#execute(argv) ⇒ Object
66
67
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
|
# File 'lib/command/backup.rb', line 66
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
62
63
64
|
# File 'lib/command/backup.rb', line 62
def path_to_relative(base, path)
path.sub("#{base}/", "")
end
|