듀에코르다의 익스트림 네가티브

when upload something through flash, content-type is always application/octet-stream. (I'm using attachment_fu) 9 개월 전

플래쉬로 업로드할 때 content-type이 항상 octet-stream 으로 나오니까... attachment_fu 쓰면 image 타입이라고 판정해주지 않아서 설정에 따라 업로드를 안받거나 썸네일을 만들지 않는 아픔이 있네요.  


mimetype_fu 라고 있어요. 파일이름으로 mimetype을 "추측"하는게죠. 뭐... 그렇죠... 다른 방법이 없네요.

 

자질구레한거 넘기고, 현재 attachment_fu와 mimetype_fu를 설치했는데 이제부터 어떻게 할까요? 라는 질문에만 답해보죠.

 

2가지 방법이 있죠.

 

attachment_fu 를 핵하는 방법과,  controller에서 어떻게든 슬기롭게 대처하는 방법...

 

=== attachment_fu의 소스는 아래와 같이 직접 패치하면 되요.


attachment_fu 플러그인이 설치된 곳에서 attachment_fu.rb 파일을 찾습니다. (ex: RAILS_ROOT/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb)


self.content_type = file_data.content_type 이런 내용이 있는 줄을 찾아 아래와 같이 수정합니다.

self.content_type = !(file_data.content_type.strip == "application/octet-stream") ? file_data.content_type : File.mime_type?(file_data.original_filename)

self.content_type = file_data['content_type'] 이런 줄이 있다면 이것 역시 아래와 같이 수정합니다.

self.content_type = !(file_data['content_type'].strip == "application/octet-stream") ? file_data.content_type : File.mime_type?(file_data.original_filename)

 

아무튼 self.content_type을 넣는 곳에는 위와 같이 octet-stream 타입이라면 mimetype_fu를 거치는 방식으로 수정합니다.

 

=== controller 에서 처리하려면...


def create
...
  _filedata = params[:file]
  _content_type = File.mime_type?(params[:Filename])
  (class << _filedata; self; end;).class_eval { define_method(:content_type) { _content_type } }
 
  @asset = Asset.new(:uploaded_data => _filedata)
  @asset.save
...
end

이렇게 해주세요.

params[:file] 은 Flash를 통해 업로드된 "File" 객체구요. params[:Filename] 역시 Flash 가 던져준 파일이름입니다. 키값은 파일업로더마다 다르겠죠? 적당히 맞춰주시면되겠네요...


===


장단점이 있겠지만, 첫번째 방법은 attachment_fu가 업데이트될 때 마다 해당 내용을 쫓아가며 수정해야한다는 점과 보이지 않는 곳이기 때문에 나중에 쉽게 잊어버릴 수 있다는 단점이 있구요.

두번째 방법은 attachment_fu와 독립적으로 돌아간다는 장점이 있는데 심적으로 뭔가 개운하지 않군요...

 

아, 세번째 방법도 있네요.

thoughtbot.compaperclip을 사용하는겁니다!

가까운 글

Games
Junks
Search
Recent Articles
Recent Comments
Blogroll