uploads: fix temp files not being cleaned up quickly enough.
Fix temp files generated during the upload process not being cleaned up quickly enough. This included downloaded files, generated preview images, and Ugoira video conversions. Before we relied on `Tempfile` cleaning up files automatically. But this only happened when the Tempfile object was garbage collected, which could take a long time. In the meantime we could have hundreds of megabytes of temp files hanging around. The fix is to explicitly close temp files when we're done with them. But the standard `Tempfile` class doesn't immediately delete the file when it's closed. So we also have to introduce a Danbooru::Tempfile wrapper that deletes the tempfile as soon as it's closed.
This commit is contained in:
@@ -7,6 +7,11 @@
|
||||
class MediaFile::Image < MediaFile
|
||||
delegate :thumbnail_image, to: :image
|
||||
|
||||
def close
|
||||
super
|
||||
@preview_frame&.close unless @preview_frame == self
|
||||
end
|
||||
|
||||
def dimensions
|
||||
image.size
|
||||
rescue Vips::Error
|
||||
@@ -107,7 +112,7 @@ class MediaFile::Image < MediaFile
|
||||
resized_image = resized_image.flatten(background: 255)
|
||||
end
|
||||
|
||||
output_file = Tempfile.new(["image-preview-#{md5}", ".#{format.to_s}"])
|
||||
output_file = Danbooru::Tempfile.new(["danbooru-image-preview-#{md5}-", ".#{format.to_s}"])
|
||||
case format.to_sym
|
||||
when :jpeg
|
||||
# https://www.libvips.org/API/current/VipsForeignSave.html#vips-jpegsave
|
||||
@@ -130,14 +135,6 @@ class MediaFile::Image < MediaFile
|
||||
preview_frame.resize!(w, h, size: :force, **options)
|
||||
end
|
||||
|
||||
def preview_frame
|
||||
if is_animated?
|
||||
FFmpeg.new(self).smart_video_preview
|
||||
else
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def is_animated?
|
||||
frame_count.to_i > 1
|
||||
end
|
||||
@@ -166,6 +163,8 @@ class MediaFile::Image < MediaFile
|
||||
false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# @return [Vips::Image] the Vips image object for the file
|
||||
def image
|
||||
Vips::Image.new_from_file(file.path, fail: false).autorot
|
||||
@@ -175,5 +174,15 @@ class MediaFile::Image < MediaFile
|
||||
FFmpeg.new(self)
|
||||
end
|
||||
|
||||
memoize :image, :video, :preview_frame, :dimensions, :error, :metadata, :is_corrupt?, :is_animated_gif?, :is_animated_png?
|
||||
def preview_frame
|
||||
@preview_frame ||= begin
|
||||
if is_animated?
|
||||
FFmpeg.new(self).smart_video_preview
|
||||
else
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
memoize :image, :video, :dimensions, :error, :metadata, :is_corrupt?, :is_animated_gif?, :is_animated_png?
|
||||
end
|
||||
|
||||
@@ -17,8 +17,9 @@ class MediaFile::Ugoira < MediaFile
|
||||
end
|
||||
|
||||
def close
|
||||
file.close
|
||||
preview_frame.close
|
||||
super
|
||||
@preview_frame&.close
|
||||
# XXX should clean up `convert` too
|
||||
end
|
||||
|
||||
def metadata
|
||||
@@ -52,7 +53,7 @@ class MediaFile::Ugoira < MediaFile
|
||||
raise RuntimeError, "can't convert ugoira to webm: no ugoira frame data was provided" unless frame_delays.present?
|
||||
|
||||
Danbooru::Archive.extract!(file) do |tmpdir, filenames|
|
||||
output_file = Tempfile.new(["ugoira-conversion", ".webm"], binmode: true)
|
||||
output_file = Danbooru::Tempfile.new(["danbooru-ugoira-conversion-#{md5}-", ".webm"], binmode: true)
|
||||
|
||||
# Duplicate last frame to avoid it being displayed only for a very short amount of time.
|
||||
last_file_name = File.basename(filenames.last)
|
||||
@@ -90,8 +91,8 @@ class MediaFile::Ugoira < MediaFile
|
||||
private
|
||||
|
||||
def preview_frame
|
||||
FFmpeg.new(convert).smart_video_preview
|
||||
@preview_frame ||= FFmpeg.new(convert).smart_video_preview
|
||||
end
|
||||
|
||||
memoize :preview_frame, :dimensions, :convert, :metadata
|
||||
memoize :dimensions, :convert, :metadata
|
||||
end
|
||||
|
||||
@@ -10,6 +10,11 @@ class MediaFile::Video < MediaFile
|
||||
:audio_stream, :audio_streams, :silence_duration, :silence_percentage, :average_loudness,
|
||||
:peak_loudness, :loudness_range, :error, to: :video
|
||||
|
||||
def close
|
||||
super
|
||||
@preview_frame&.close
|
||||
end
|
||||
|
||||
def dimensions
|
||||
[video.width, video.height]
|
||||
end
|
||||
@@ -65,8 +70,8 @@ class MediaFile::Video < MediaFile
|
||||
end
|
||||
|
||||
def preview_frame
|
||||
video.smart_video_preview
|
||||
@preview_frame ||= video.smart_video_preview
|
||||
end
|
||||
|
||||
memoize :video, :preview_frame, :dimensions, :metadata, :duration, :has_audio?
|
||||
memoize :video, :dimensions, :metadata, :duration, :has_audio?
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user