lib/extension.rb in narou-3.2.5.1 vs lib/extension.rb in narou-3.3.0

- old
+ new

@@ -1,6 +1,11 @@ +# frozen_string_literal: true +# +# Copyright 2013 whiteleaf. All rights reserved. +# + require "open-uri" require "openssl" # open-uri で http → https へのリダイレクトを有効にする require "open_uri_redirections" @@ -16,24 +21,21 @@ # ファイルに直接上書きしないで、一旦別名で作成してからファイル名変更をすることで、 # ファイル書き込み中のPCクラッシュ等でデータが飛ばない様にする # require "securerandom" -def File.write(path, string, *options) +def File.write(path, string, *options, mode: nil) + return super if mode + dirpath = File.dirname(path) - backup = false - temp_path = - if File.extname(path) == ".yaml" && File.basename(dirpath) != Downloader::SECTION_SAVE_DIR_NAME - backup = true - "#{path}.backup" - else - File.join(dirpath, SecureRandom.hex(15)) - end + temp_path = File.join(dirpath, SecureRandom.hex(15)) + if File.extname(path) == ".yaml" && File.basename(dirpath) != Downloader::SECTION_SAVE_DIR_NAME + backup = "#{path}.backup" + end - super(temp_path, string, *options) - + res = super(temp_path, string, *options) if backup - super(path, string, *options) - else - File.rename(temp_path, path) + super(backup, string, *options) end + File.rename(temp_path, path) + res end