Skip to content

Commit

Permalink
Merge pull request #371 from azul/bugfix/symlink-if-needed-only
Browse files Browse the repository at this point in the history
only create a symlink for public asset dirs if needed
  • Loading branch information
azul committed Jan 13, 2016
2 parents 57f9599 + 984e83d commit 340629c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
13 changes: 5 additions & 8 deletions app/controllers/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ def fetch_asset

# Update access and redirect to the same path - which will now have a
# symlink in public
# This only applies iff asset is public AND the public file is not in place.
def symlink_public_asset
return unless @asset.public? and !File.exist?(@asset.public_filename)
@asset.update_access
@asset.generate_thumbnails
raise_not_found(:file.t) unless @asset.thumbnails.any?

# redirect to the same url again, but next time they will get the symlinks
redirect_to
if @asset.symlink_missing?
@asset.update_access
# redirect to the same url again, but next time they will get the symlinks
redirect_to
end
end

## Helper Methods
Expand Down
13 changes: 11 additions & 2 deletions app/models/asset_extension/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,18 @@ def current_data
File.file?(private_filename) ? File.read(private_filename) : nil
end

def symlink_missing?
self.public? and !has_symlink?
end
public :symlink_missing?

def has_symlink?
File.exist?(File.dirname(public_filename))
end

# creates a symlink from the private asset storage to a publicly accessible directory
def add_symlink
unless File.exist?(File.dirname(public_filename))
unless has_symlink?
real_private_path = Pathname.new(private_filename).realpath.dirname
real_public_path = Pathname.new(public_storage).realpath
public_to_private = real_private_path.relative_path_from(real_public_path)
Expand All @@ -231,7 +240,7 @@ def add_symlink

# removes symlink from public directory
def remove_symlink
if File.exist?(File.dirname(public_filename))
if has_symlink?
FileUtils.rm(File.dirname(public_filename))
end
end
Expand Down

0 comments on commit 340629c

Please sign in to comment.