commit c232e62306fd1ce21ea12eb254e450bbdc64b91d
parent 9eea35ecd4c52d5589f105d9d0749e79b2fd7139
Author: David Eisinger <[email protected]>
Date: Fri, 2 Feb 2024 15:59:55 -0500
Don't use tempfiles
Diffstat:
2 files changed, 7 insertions(+), 21 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -2,4 +2,3 @@
public
resources/_gen
secret.key
-bin/dither/tmp
diff --git a/bin/dither/dither.rb b/bin/dither/dither.rb
@@ -6,34 +6,24 @@ MiniMagick.logger.level = Logger::DEBUG
ROOT = ENV.fetch("ROOT")
KEY = ENV.fetch("KEY")
DITHER = ENV["DITHER"] != "0"
-
-EXTENSION, CONTENT_TYPE = if DITHER
- [".png", "image/png"]
-else
- [".webp", "image/webp"]
-end
-
-FileUtils.mkdir_p "tmp"
+FORMAT = DITHER ? "png" : "webp"
get "/*" do |path|
- filename = File.basename(path)
- geometry = params["geo"] unless params["geo"] == ""
+ content_type "image/#{FORMAT}"
- @decrypted = Tempfile.new(filename, "tmp")
- @dithered = Tempfile.new([filename, EXTENSION], "tmp")
+ geometry = params["geo"] unless params["geo"] == ""
- %x(
+ decrypted = %x(
openssl \
aes-256-cbc \
-d \
-in #{ROOT}/#{path}.enc \
- -out #{@decrypted.path} \
-pass file:#{KEY} \
-iter 1000000
)
convert = MiniMagick::Tool::Convert.new
- convert << @decrypted.path
+ convert.stdin
convert.background("white")
convert.layers("flatten")
@@ -52,9 +42,6 @@ get "/*" do |path|
convert.monochrome
end
- convert << @dithered.path
- convert.call
-
- content_type CONTENT_TYPE
- File.open(@dithered.path)
+ convert << "#{FORMAT.upcase}:-"
+ convert.call(stdin: decrypted)
end