문제

나는 자산을 모델에 첨부하기 위해 Ruby on Rails와 함께 Paplip을 사용하고 있습니다. 이러한 자산은 모든 파일 유형 일 수 있으며 현재 썸네일은 자산이 이미지 인 경우에만 생성됩니다. 업로드시 파일의 축소판을 생성하거나 Default_Url로 무언가를 설정하여 다른 파일에 대해 다른 기본 이미지를 표시 할 수 있지만 지금까지는이 작업에 도움이되는 리소스를 찾을 수 없습니다. 나 자신이 어디로도받지 못하고 있습니다.

내 모델은 다음과 같습니다.

  class Asset < ActiveRecord::Base  
    has_attached_file :media,  
    :storage => :s3,  
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",  
    :path => ":attachment/:id/:style.:extension",  
    :bucket => S3_BUCKET,  
    :styles => {:thumb => "75x75>", :large => "600x800>",  
    :whiny => false,  
    :default_url => "/images/:attachment/missing.jpg"  

생성이 실패한 경우 사용자 정의 썸네일을 생성하기위한 리소스가 있거나 다음과 같은 내용으로 되돌아 가면 : 기본 URL의 content_type? 나는 소스를 살펴보고 아무데도 갈 수 없었습니다.

감사!

도움이 되었습니까?

해결책

나는 실제로이 동일한 기능을 구현했습니다. PaperClip은 모든 이미지 및 PDF에 대한 축소판을 생성하며 MS Word, Excel, HTML, TXT 파일 등에 대한 사용자 정의 썸네일 아이콘을 추가했습니다.

내 솔루션은 매우 간단합니다. 내 모델에서 Attachment (당신의 경우 Asset) 다음 방법을 정의했습니다.

def thumbnail_uri(style = :original)
  if style == :original || has_thumbnail?
    attachment.s3.interface.get_link(attachment.s3_bucket.to_s, attachment.path(style), EXPIRES_AFTER)
  else
    generic_icon_path style
  end
end

이는 URL을 S3에 저장된 썸네일로 반환하거나 자산 콘텐츠 유형 (아래에 설명)을 기반으로 일반 PNG 아이콘으로의 로컬 경로를 반환합니다. 그만큼 has_thumbnail? 메소드는이 자산에 썸네일이 생성되었는지 여부를 결정합니다. 이것은 내가 자신의 종이 클립 포크에 추가 한 것이지만, 당신은 당신 자신의 논리로 대체 할 수 있습니다 (나는 이것을 결정하는 '표준'방법은 확실하지 않습니다. 경로를 정의 된 '누락 된'경로와 비교할 수 있습니다. 컨텐츠 유형을 기본 목록과 비교하는 것만으로 [ "image/jpeg", "image/png"등).

어쨌든 다음은 썸네일 스타일 (케이스 : 썸 및 : 큰)과 컨텐츠 유형을 기반으로 일반 아이콘으로가는 경로를 전달하는 방법입니다.

# Generates a path to the thumbnail image for the given content type 
# and image size.
#
# e.g. a :small thumbnail with a content type of text/html, the file name 
#      would have the filename icon.small.text.html.png
#
# If no such thumbnail can be found a generic one is returned
def generic_icon_path(style = image.default_style)
  url = "/images/attachments/icon.#{style.to_s}.#{attachment_content_type.sub('/', '.')}.png"
  if File.exists? "#{RAILS_ROOT}/public/#{url}"
    url
  else
    "/images/attachments/icon.#{style.to_s}.default.png"
  end
end

그런 다음 새 썸네일을 추가하려면 PNG 파일을 /images/attachments/ 올바른 파일 이름 컨벤션으로. 내 Thumbail 스타일은 : Small과 Word, Excel 및 Plain Text의 스타일을 정의했습니다. 현재 나는 다음과 같습니다.

icon.small.application.msword.png
icon.small.text.plain.png
icon.small.application.vnd.ms-excel.png
icon.small.application.vnd.openxmlformats-officedocument.spreadsheetml.sheet.png
icon.small.application.vnd.openxmlformats-officedocument.wordprocessingml.document.png

컨텐츠 유형이 지원되지 않으면 일반적인 'Catch All'아이콘이 표시됩니다.

icon.small.default.png

다른 팁

자산에서 일부 파일 유형을 상속받을 수 있으며 예를 들어 비디오를 지정할 수 있습니다.

has_attached_file : media, ..., : : style => {....}

이 튜토리얼을 살펴보십시오 비디오 썸네일.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top