-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow a Fog::Storage uploader to be passed in for uploading of genera…
…ted tiles rather than local storage
- Loading branch information
1 parent
d027a89
commit 9ab9b43
Showing
10 changed files
with
158 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
### Next | ||
|
||
* [#5](https://github.com/dblock/dzt/pull/5) Allow a Fog::Storage uploader to be passed in for uploading of generated tiles rather than local storage | ||
|
||
### 0.1.0 (3/16/2014) | ||
|
||
* Initial public release - [@dblock](https://github.com/dblock). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ gemspec | |
|
||
gem "rspec" | ||
gem "rake" | ||
gem "fog" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module DZT | ||
class FileStorage | ||
# | ||
# @param overwrite: If true, overwrites existing tiles, defaults to false. | ||
# @param destination: Full directory in which to output tiles, defaults to 'tiles' in the current dir. | ||
# | ||
def initialize(options = {}) | ||
@overwrite = options[:overwrite] || false | ||
@store_path = options[:destination] || File.join(Dir.pwd, 'tiles') | ||
end | ||
|
||
def exists? | ||
! @overwrite && File.directory?(@store_path) && ! Dir["@{@store_path}/*"].empty? | ||
end | ||
|
||
def storage_location(level) | ||
File.join(@store_path, level.to_s) | ||
end | ||
|
||
def mkdir(path) | ||
FileUtils.mkdir_p(path) | ||
end | ||
|
||
def write(file, dest, options = {}) | ||
quality = options[:quality] | ||
file.write(dest) { @quality = quality if quality } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module DZT | ||
class S3Storage | ||
# | ||
# @param s3_acl: ACL to use for storing, defaults to 'public-read'. | ||
# @param s3_bucket: Bucket to store tiles. | ||
# @param s3_key: Key to prefix stored files. | ||
# @param aws_id: AWS Id. | ||
# @param aws_secret: AWS Secret. | ||
# | ||
def initialize(options = {}) | ||
@s3_acl = options[:s3_acl] || 'public-read' | ||
@s3_bucket = options[:s3_bucket] | ||
@s3_key = options[:s3_key] | ||
@s3_id = options[:aws_id] | ||
@s3_secret = options[:aws_secret] | ||
end | ||
|
||
def s3 | ||
@s3 ||= Fog::Storage.new( | ||
provider: 'AWS', | ||
aws_access_key_id: @s3_id, | ||
aws_secret_access_key: @s3_secret | ||
) | ||
end | ||
|
||
# Currently does not supporting checking S3 fo overwritten files | ||
def exists? | ||
false | ||
end | ||
|
||
def storage_location(level) | ||
"#{@s3_key}/#{level.to_s}" | ||
end | ||
|
||
# no-op | ||
def mkdir(path) | ||
end | ||
|
||
def write(file, dest, options = {}) | ||
quality = options[:quality] | ||
@s3.put_object(@s3_bucket, dest, file.to_blob { @quality = quality if quality }, | ||
'Content-Type' => file.mime_type, | ||
'x-amz-acl' => @s3_acl | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,65 @@ | ||
require 'spec_helper' | ||
require 'RMagick' | ||
include Magick | ||
|
||
describe DZT::Tiler do | ||
before :each do | ||
@fixtures_dir = File.expand_path(File.join(__FILE__, '../../fixtures')) | ||
end | ||
it "slices an image" do | ||
Dir.mktmpdir do |tmpdir| | ||
tiler = DZT::Tiler.new( | ||
context 'storing files locally' do | ||
it "slices an image and stores files" do | ||
Dir.mktmpdir do |tmpdir| | ||
storage = DZT::FileStorage.new(destination: tmpdir) | ||
tiler = DZT::Tiler.new( | ||
source: File.join(@fixtures_dir, "francisco-jose-de-goya-y-lucientes-senora-sabasa-garcia.jpg"), | ||
storage: storage | ||
) | ||
tiler.slice! | ||
expect(Dir["#{tmpdir}/*"].map { |dir| dir.split("/").last.to_i }.sort).to eq((0..12).to_a) | ||
# center | ||
image = Magick::Image::read("#{tmpdir}/11/1_1.jpg").first | ||
expect(image.columns).to eq(512) | ||
expect(image.rows).to eq(512) | ||
# edge | ||
image = Magick::Image::read("#{tmpdir}/11/2_2.jpg").first | ||
expect(image.columns).to eq(168) | ||
expect(image.rows).to eq(443) | ||
end | ||
end | ||
end | ||
context 'uploading resultant files to S3' do | ||
before :each do | ||
Fog.mock! | ||
Fog::Mock.reset | ||
storage = DZT::S3Storage.new( | ||
s3_acl: 'public-read', | ||
s3_bucket: 'tiled-images', | ||
s3_key: 'dztiles', | ||
aws_id: 'id', | ||
aws_secret: 'secret' | ||
) | ||
@bucket = storage.s3.directories.create(key: 'tiled-images') | ||
@tiler = DZT::Tiler.new( | ||
source: File.join(@fixtures_dir, "francisco-jose-de-goya-y-lucientes-senora-sabasa-garcia.jpg"), | ||
destination: tmpdir | ||
storage: storage | ||
) | ||
tiler.slice! | ||
Dir["#{tmpdir}/*"].map { |dir| dir.split("/").last.to_i }.sort.should == (0..12).to_a | ||
# center | ||
image = Magick::Image::read("#{tmpdir}/11/1_1.jpg").first | ||
image.columns.should == 512 | ||
image.rows.should == 512 | ||
# edge | ||
image = Magick::Image::read("#{tmpdir}/11/2_2.jpg").first | ||
image.columns.should == 168 | ||
image.rows.should == 443 | ||
end | ||
it 'slices the images' do | ||
@tiler.slice! | ||
file = @bucket.files.select { |f| f.key == 'dztiles/11/1_1.jpg' }.first | ||
image = Image.from_blob(file.body).first | ||
expect(image.columns).to eq(512) | ||
expect(image.rows).to eq(512) | ||
file = @bucket.files.select { |f| f.key == 'dztiles/11/2_2.jpg' }.first | ||
image = Image.from_blob(file.body).first | ||
expect(image.columns).to eq(168) | ||
expect(image.rows).to eq(443) | ||
end | ||
it 'stores the files properly' do | ||
expect_any_instance_of(Fog::Storage::AWS::Mock).to receive(:put_object).at_least(53).times do |*args| | ||
expect(args.last).to eq("Content-Type" => "image/jpeg", "x-amz-acl" => "public-read") | ||
end | ||
@tiler.slice! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
describe DZT do | ||
it "has a version" do | ||
DZT::VERSION.should_not be_nil | ||
expect(DZT::VERSION).to_not be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
require 'rspec' | ||
require 'tmpdir' | ||
require 'dzt' | ||
require 'fog' |