Faraday::UploadIO テレグラム API の後にファイルを別のフォルダーに移動できません
概要
画像を含むフォルダーを繰り返し処理し、ファイル サイズに収まらない場合は画像を圧縮し、圧縮バージョンを写真として送信し、非圧縮バージョンをファイルとして送信します。次に、アップロードしたファイルを別のフォルダーに移動しようとしましたが、エラーが発生しました アクセス許可が拒否されました @ rb_file_s_rename - (95880631_p0.jpg、uploaded\95880631_p0.jpg) (Errno::EACCES)。
以下の私のアップロードコード
Dir.glob("*.{jpg,png}").each do |file|
p file
if file.end_with? ".jpg"
exten='image/jpeg'
else
exten="image/png"
end
img_size=0
check_image=MiniMagick::Image.open("#{file}")
if check_image.dimensions.max>9000
img_size=9000
else
img_size=check_image.dimensions.max
end
if check_image.size>5000000
p "compressing #{file}"
system 'mogrify -define jpeg:extent=5000kb -resize '+"#{img_size}x#{img_size}"+' -path compress -format jpg '+ "#{file}" if !File.exist?('compress\\'+"#{file[..file.index(".")-1]}.jpg")
check_image='compress\\'+"#{file[..file.index(".")-1]}.jpg"
else
check_image=file
end
if file.match?(/[0-9]_p[0-9]\.(jpg|png)/)#(/_p[0-9]+\./)
bot.api.send_photo(
chat_id: channel_id,
photo: Faraday::UploadIO.new(check_image, exten),
parse_mode:"MarkdownV2",
caption: "[Source pixiv](https://www.pixiv.net/en/artworks/#{file[..file.index("_p")-1]})"
)
else
bot.api.send_photo(
chat_id: channel_id,
photo: Faraday::UploadIO.new(check_image, exten)
)
end
bot.api.send_document(chat_id:channel_id, document: Faraday::UploadIO.new(file, exten))
FileUtils.mv(file, "uploaded\\#{file}")
end
MiniMagick::Image.open も Dir.glob もこのエラーを引き起こさないことを確認しましたが、Faraday::UploadIO 部分を追加した場合にのみ発生します。
ファイルに影響を与えないようにUploadIOを「閉じる」方法はありますか?それともそれを機能させるために何か他のことをする必要がありますか?
解決策
UploadIO を関数から分離することで問題を修正しました。
upld=Faraday::UploadIO.new(check_image, exten)
if file.match?(/[0-9]_p[0-9]\.(jpg|png)/)#(/_p[0-9]+\./)
bot.api.send_photo(
chat_id: channel_id,
photo: upld,
parse_mode:"MarkdownV2",
caption: "[Source pixiv](https://www.pixiv.net/en/artworks/#{file[..file.index("_p")-1]})"
)
else
bot.api.send_photo(
chat_id: channel_id,
photo: upld
)
end
upld.close
upld=Faraday::UploadIO.new(file, exten)
bot.api.send_document(chat_id:channel_id, document: upld)
upld.close
FileUtils.mv(file, "uploaded\\#{file}")