From 8e7bfd0f29b9973dc145a2fce8be14d2ec5582d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 28 May 2015 01:03:24 +0200 Subject: [PATCH] FIX: automatically growing uploads tree --- app/models/optimized_image.rb | 3 +++ lib/file_store/base_store.rb | 16 ++++++++++++++++ lib/file_store/local_store.rb | 17 ++++------------- lib/file_store/s3_store.rb | 17 ++--------------- spec/components/cooked_post_processor_spec.rb | 4 ++-- spec/components/email/receiver_spec.rb | 2 +- spec/components/file_store/local_store_spec.rb | 12 +++++------- spec/components/file_store/s3_store_spec.rb | 13 +++++-------- 8 files changed, 38 insertions(+), 46 deletions(-) diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb index 227b8ffcfd5..4600af8e06f 100644 --- a/app/models/optimized_image.rb +++ b/app/models/optimized_image.rb @@ -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 diff --git a/lib/file_store/base_store.rb b/lib/file_store/base_store.rb index 6fc9e688573..c5b2d80cb64 100644 --- a/lib/file_store/base_store.rb +++ b/lib/file_store/base_store.rb @@ -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 diff --git a/lib/file_store/local_store.rb b/lib/file_store/local_store.rb index 2fce96c365a..1d009c17845 100644 --- a/lib/file_store/local_store.rb +++ b/lib/file_store/local_store.rb @@ -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) diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb index 846a5083698..2570d4782cf 100644 --- a/lib/file_store/s3_store.rb +++ b/lib/file_store/s3_store.rb @@ -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 diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 33403f154c8..916608de74c 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -118,7 +118,7 @@ describe CookedPostProcessor do it "generates overlay information" do cpp.post_process_images - expect(cpp.html).to match_html '