Skip to content

Commit

Permalink
Add configuration to change temporary directory (#541)
Browse files Browse the repository at this point in the history
* Add configuration to change temporary directory

Example:

  MiniMagick.configure do |config|
    config.tmpdir = File.join(Dir.tmpdir, "/my/new/tmp_dir")
  end

Defaults to Dir.tmpdir

* Add documentation to README file
  • Loading branch information
willianveiga committed May 28, 2022
1 parent 42e9d01 commit 67903eb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,20 @@ setting the following variables in your application's process environment:
For a full list of variables and description, see [ImageMagick's resources
documentation](http://www.imagemagick.org/script/resources.php#environment).

## Changing temporary directory

ImageMagick allows you to change the temporary directory to process the image file:

```rb
MiniMagick.configure do |config|
config.tmpdir = File.join(Dir.tmpdir, "/my/new/tmp_dir")
end
```

The example directory `/my/new/tmp_dir` must exist and must be writable.

If not configured, it will default to `Dir.tmpdir`.

## Troubleshooting

### Errors being raised when they shouldn't
Expand Down
8 changes: 8 additions & 0 deletions lib/mini_magick/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ module Configuration
# @return [Logger]
#
attr_accessor :logger
##
# Temporary directory used by MiniMagick, default is `Dir.tmpdir`, but
# you can override it.
#
# @return [String]
#
attr_accessor :tmpdir

##
# If set to `true`, it will `identify` every newly created image, and raise
Expand Down Expand Up @@ -82,6 +89,7 @@ module Configuration
attr_accessor :shell_api

def self.extended(base)
base.tmpdir = Dir.tmpdir
base.validate_on_create = true
base.validate_on_write = true
base.whiny = true
Expand Down
2 changes: 1 addition & 1 deletion lib/mini_magick/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def which(cmd)
end

def tempfile(extension)
Tempfile.new(["mini_magick", extension]).tap do |tempfile|
Tempfile.new(["mini_magick", extension], MiniMagick.tmpdir).tap do |tempfile|
tempfile.binmode
yield tempfile if block_given?
tempfile.close
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/mini_magick/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ def create(path = image_path)
expect { create(image_path(:not)) }
.not_to raise_error
end

context "when a tmpdir is configured" do
before { FileUtils.mkdir_p(new_tmp_dir) }
after { FileUtils.rm_rf(new_tmp_dir) }

let(:new_tmp_dir) { File.join(Dir.tmpdir, "new_tmp_dir") }

it "uses the tmpdir to create the file" do
allow(MiniMagick).to receive(:tmpdir).and_return(new_tmp_dir)
image = create
expect(File.dirname(image.path)).to eq new_tmp_dir
end
end
end

describe "#initialize" do
Expand Down

0 comments on commit 67903eb

Please sign in to comment.