FIX: automatically growing uploads tree

This commit is contained in:
Régis Hanol 2015-05-28 01:03:24 +02:00
parent 1b96a3acc1
commit 8e7bfd0f29
8 changed files with 38 additions and 46 deletions

View File

@ -3,6 +3,9 @@ require "digest/sha1"
class OptimizedImage < ActiveRecord::Base
belongs_to :upload
# BUMP UP if optimized image algorithm changes
VERSION = 1
def self.create_for(upload, width, height, opts={})
return unless width > 0 && height > 0

View File

@ -48,6 +48,22 @@ module FileStore
url
end
def get_path_for(type, id, sha, extension)
depth = [0, Math.log(id / 1_000.0, 16).ceil].max
tree = File.join(*sha[0, depth].split(""), "")
"#{type}/#{depth + 1}X/#{tree}#{sha}#{extension}"
end
def get_path_for_upload(upload)
get_path_for("original".freeze, upload.id, upload.sha1, upload.extension)
end
def get_path_for_optimized_image(optimized_image)
upload = optimized_image.upload
extension = "_#{OptimizedImage::VERSION}_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
get_path_for("optimized".freeze, upload.id, upload.sha1, extension)
end
end
end

View File

@ -5,12 +5,12 @@ module FileStore
class LocalStore < BaseStore
def store_upload(file, upload, content_type = nil)
path = get_path_for_upload(file, upload)
path = get_path_for_upload(upload)
store_file(file, path)
end
def store_optimized_image(file, optimized_image)
path = get_path_for_optimized_image(file, optimized_image)
path = get_path_for_optimized_image(optimized_image)
store_file(file, path)
end
@ -57,17 +57,8 @@ module FileStore
private
def get_path_for_upload(file, upload)
get_path_for("original".freeze, upload.sha1, upload.extension)
end
def get_path_for_optimized_image(file, optimized_image)
extension = "_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
get_path_for("optimized".freeze, optimized_image.sha1, extension)
end
def get_path_for(type, sha, extension)
"#{relative_base_url}/#{type}/#{sha[0]}/#{sha[1]}/#{sha}#{extension}"
def get_path_for(type, upload_id, sha, extension)
"#{relative_base_url}/#{super(type, upload_id, sha, extension)}"
end
def store_file(file, path)

View File

@ -14,12 +14,12 @@ module FileStore
end
def store_upload(file, upload, content_type=nil)
path = get_path_for_upload(file, upload)
path = get_path_for_upload(upload)
store_file(file, path, filename: upload.original_filename, content_type: content_type, cache_locally: true)
end
def store_optimized_image(file, optimized_image)
path = get_path_for_optimized_image(file, optimized_image)
path = get_path_for_optimized_image(optimized_image)
store_file(file, path)
end
@ -94,19 +94,6 @@ module FileStore
private
def get_path_for_upload(file, upload)
get_path_for("original".freeze, upload.sha1, upload.extension)
end
def get_path_for_optimized_image(file, optimized_image)
extension = "_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
get_path_for("optimized".freeze, optimized_image.sha1, extension)
end
def get_path_for(type, sha, extension)
"#{type}/#{sha[0]}/#{sha[1]}/#{sha}#{extension}"
end
# options
# - filename
# - content_type

View File

@ -118,7 +118,7 @@ describe CookedPostProcessor do
it "generates overlay information" do
cpp.post_process_images
expect(cpp.html).to match_html '<div class="lightbox-wrapper"><a data-download-href="/uploads/default/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98" href="/uploads/default/1/1234567890123456.jpg" class="lightbox" title="logo.png"><img src="/uploads/default/optimized/d/a/da39a3ee5e6b4b0d3255bfef95601890afd80709_690x1380.png" width="690" height="1380"><div class="meta">
expect(cpp.html).to match_html '<div class="lightbox-wrapper"><a data-download-href="/uploads/default/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98" href="/uploads/default/1/1234567890123456.jpg" class="lightbox" title="logo.png"><img src="/uploads/default/optimized/2X/e/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98_1_690x1380.png" width="690" height="1380"><div class="meta">
<span class="filename">logo.png</span><span class="informations">1000x2000 1.21 KB</span><span class="expand"></span>
</div></a></div>'
expect(cpp).to be_dirty
@ -145,7 +145,7 @@ describe CookedPostProcessor do
it "generates overlay information" do
cpp.post_process_images
expect(cpp.html).to match_html '<div class="lightbox-wrapper"><a data-download-href="/uploads/default/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98" href="/uploads/default/1/1234567890123456.jpg" class="lightbox" title="WAT"><img src="/uploads/default/optimized/d/a/da39a3ee5e6b4b0d3255bfef95601890afd80709_690x1380.png" title="WAT" width="690" height="1380"><div class="meta">
expect(cpp.html).to match_html '<div class="lightbox-wrapper"><a data-download-href="/uploads/default/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98" href="/uploads/default/1/1234567890123456.jpg" class="lightbox" title="WAT"><img src="/uploads/default/optimized/2X/e/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98_1_690x1380.png" title="WAT" width="690" height="1380"><div class="meta">
<span class="filename">WAT</span><span class="informations">1000x2000 1.21 KB</span><span class="expand"></span>
</div></a></div>'
expect(cpp).to be_dirty

View File

@ -309,7 +309,7 @@ This is a link http://example.com"
receiver.process
expect(topic.posts.count).to eq(start_count + 1)
expect(topic.posts.last.cooked).to match /<img src=['"](\/uploads\/default\/original\/#{upload_sha[0]}\/#{upload_sha[1]}\/#{upload_sha}\.png)['"] width=['"]289['"] height=['"]126['"]>/
expect(topic.posts.last.cooked).to match /<img src=['"](\/uploads\/default\/original\/.+\.png)['"] width=['"]289['"] height=['"]126['"]>/
expect(Upload.find_by(sha1: upload_sha)).not_to eq(nil)
end

View File

@ -5,20 +5,18 @@ describe FileStore::LocalStore do
let(:store) { FileStore::LocalStore.new }
let(:upload) { build(:upload) }
let(:upload) { Fabricate(:upload) }
let(:uploaded_file) { file_from_fixtures("logo.png") }
let(:optimized_image) { build(:optimized_image) }
let(:optimized_image) { Fabricate(:optimized_image) }
let(:avatar) { build(:upload) }
let(:avatar) { Fabricate(:upload) }
describe ".store_upload" do
it "returns a relative url" do
Time.stubs(:now).returns(Time.utc(2013, 2, 17, 12, 0, 0, 0))
upload.stubs(:id).returns(42)
store.expects(:copy_file)
expect(store.store_upload(uploaded_file, upload)).to eq("/uploads/default/original/e/9/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98.png")
expect(store.store_upload(uploaded_file, upload)).to eq("/uploads/default/original/2X/e/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98.png")
end
end
@ -27,7 +25,7 @@ describe FileStore::LocalStore do
it "returns a relative url" do
store.expects(:copy_file)
expect(store.store_optimized_image({}, optimized_image)).to eq("/uploads/default/optimized/8/6/86f7e437faa5a7fce15d1ddcb9eaeaea377667b8_100x200.png")
expect(store.store_optimized_image({}, optimized_image)).to eq("/uploads/default/optimized/2X/e/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98_#{OptimizedImage::VERSION}_100x200.png")
end
end

View File

@ -7,13 +7,13 @@ describe FileStore::S3Store do
let(:s3_helper) { stub }
let(:store) { FileStore::S3Store.new(s3_helper) }
let(:upload) { build(:upload) }
let(:upload) { Fabricate(:upload) }
let(:uploaded_file) { file_from_fixtures("logo.png") }
let(:optimized_image) { build(:optimized_image) }
let(:optimized_image) { Fabricate(:optimized_image) }
let(:optimized_image_file) { file_from_fixtures("logo.png") }
let(:avatar) { build(:upload) }
let(:avatar) { Fabricate(:upload) }
before(:each) do
SiteSetting.stubs(:s3_upload_bucket).returns("S3_Upload_Bucket")
@ -24,10 +24,8 @@ describe FileStore::S3Store do
describe ".store_upload" do
it "returns an absolute schemaless url" do
upload.stubs(:id).returns(42)
upload.stubs(:extension).returns(".png")
s3_helper.expects(:upload)
expect(store.store_upload(uploaded_file, upload)).to eq("//s3_upload_bucket.s3.amazonaws.com/original/e/9/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98.png")
expect(store.store_upload(uploaded_file, upload)).to eq("//s3_upload_bucket.s3.amazonaws.com/original/2X/e/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98.png")
end
end
@ -35,9 +33,8 @@ describe FileStore::S3Store do
describe ".store_optimized_image" do
it "returns an absolute schemaless url" do
optimized_image.stubs(:id).returns(42)
s3_helper.expects(:upload)
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq("//s3_upload_bucket.s3.amazonaws.com/optimized/8/6/86f7e437faa5a7fce15d1ddcb9eaeaea377667b8_100x200.png")
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq("//s3_upload_bucket.s3.amazonaws.com/optimized/2X/e/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98_#{OptimizedImage::VERSION}_100x200.png")
end
end