Skip to content

Commit

Permalink
FEATURE: use original filename when clicking the download link in the…
Browse files Browse the repository at this point in the history
… lightbox
  • Loading branch information
ZogStriP committed Oct 15, 2014
1 parent 4762b4a commit 31e9caf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
3 changes: 2 additions & 1 deletion app/assets/javascripts/discourse/lib/lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ Discourse.Lightbox = {

image: {
titleSrc: function(item) {
var href = item.el.data("download-href") || item.src;
return [
item.el.attr("title"),
$("span.informations", item.el).text().replace('x', '×'),
'<a class="image-source-link" href="' + item.src + '" target="_blank">' + I18n.t("lightbox.download") + '</a>'
'<a class="image-source-link" href="' + href + '">' + I18n.t("lightbox.download") + '</a>'
].join(' &middot; ');
}
}
Expand Down
35 changes: 21 additions & 14 deletions lib/cooked_post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ def add_lightbox!(img, original_width, original_height, upload=nil)
# then, the link to our larger image
a = Nokogiri::XML::Node.new("a", @doc)
img.add_next_sibling(a)

if upload && Discourse.store.internal?
a["data-download-href"] = Discourse.store.download_url(upload)
end

a["href"] = img["src"]
a["class"] = "lightbox"
a.add_child(img)
Expand Down Expand Up @@ -217,9 +222,11 @@ def post_process_oneboxes
end

def optimize_urls
@doc.css("a[href]").each do |a|
href = a["href"].to_s
a["href"] = schemaless absolute(href) if is_local(href)
%w{href data-download-href}.each do |selector|
@doc.css("a[#{selector}]").each do |a|
href = a["#{selector}"].to_s
a["#{selector}"] = schemaless absolute(href) if is_local(href)
end
end

@doc.css("img[src]").each do |img|
Expand All @@ -243,17 +250,17 @@ def pull_hotlinked_images(bypass_bump = false)
end

def disable_if_low_on_disk_space
if available_disk_space < SiteSetting.download_remote_images_threshold
SiteSetting.download_remote_images_to_local = false
# log the site setting change
reason = I18n.t("disable_remote_images_download_reason")
staff_action_logger = StaffActionLogger.new(Discourse.system_user)
staff_action_logger.log_site_setting_change("download_remote_images_to_local", true, false, { details: reason })
# also send a private message to the site contact user
SystemMessage.create_from_system_user(Discourse.site_contact_user, :download_remote_images_disabled)
return true
end
false
return false if available_disk_space >= SiteSetting.download_remote_images_threshold

SiteSetting.download_remote_images_to_local = false
# log the site setting change
reason = I18n.t("disable_remote_images_download_reason")
staff_action_logger = StaffActionLogger.new(Discourse.system_user)
staff_action_logger.log_site_setting_change("download_remote_images_to_local", true, false, { details: reason })
# also send a private message to the site contact user
SystemMessage.create_from_system_user(Discourse.site_contact_user, :download_remote_images_disabled)

true
end

def available_disk_space
Expand Down
3 changes: 3 additions & 0 deletions lib/file_store/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def absolute_base_url
def relative_base_url
end

def download_url(upload)
end

def external?
end

Expand Down
5 changes: 5 additions & 0 deletions lib/file_store/local_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def relative_base_url
"/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
end

def download_url(upload)
return unless upload
"#{relative_base_url}/#{upload.sha1}"
end

def external?
!internal?
end
Expand Down
1 change: 1 addition & 0 deletions plugins/emoji/public/public
2 changes: 1 addition & 1 deletion spec/components/cooked_post_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

it "generates overlay information" do
cpp.post_process_images
cpp.html.should match_html '<div class="lightbox-wrapper"><a href="/uploads/default/1/1234567890123456.jpg" class="lightbox" title="logo.png"><img src="/uploads/default/_optimized/da3/9a3/ee5e6b4b0d_690x1380.png" width="690" height="1380"><div class="meta">
cpp.html.should 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/da3/9a3/ee5e6b4b0d_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>'
cpp.should be_dirty
Expand Down

0 comments on commit 31e9caf

Please sign in to comment.