require 'digest/sha1' require "uri" require "models/tag.rb" require "models/comment.rb" require "models/rating.rb" require "models/album.rb" require "models/media_file.rb" class Mediaitem < NotRelational::DomainModel extend NotRelational::Geo::Locatable include NotRelational::Geo::Locatable property :id,:string,:is_primary_key=>true property :guid,:string,:unique=>true property :group_id , :string property :title , :string property :user_login , :string property :description , :clob property :latitude , :float property :longitude , :float property :address , :string property :bucket , :string property :length_in_seconds , :unsigned_integer property :HasVideo , :boolean property :HasAudio , :boolean property :HasImage , :boolean property :is_private , :boolean property :created_time ,:date has_many :Mediafile,nil,nil,:dependent=>:destroy has_many :Tag,nil,nil,:dependent=>:destroy has_many :Comment,nil,nil,:dependent=>:destroy has_many :Rating,nil,nil,:dependent=>:destroy has_many :AlbumMediaItem,nil,nil,:dependent=>:destroy index :hasimage_and_is_private,[:HasImage,:is_private] index :hasvideo_and_is_private,[:HasVideo,:is_private] index :hasaudio_and_is_private,[:HasAudio,:is_private] index :hasaudio_hasvideo_and_is_private,[:HasAudio,:HasVideo,:is_private] many_to_many :Album,:AlbumMediaItem,:mediaitem_id,:album_id,:albums belongs_to :Group def Mediaitem.recent(how_many=24) Mediaitem.find(:all,:order_by => :created_time,:order => :descending,:limit => how_many) end def Mediaitem.recent_video(how_many=24) Mediaitem.find_by_hasvideo_and_is_private( true, false , :order_by => :created_time, :order => :descending, :limit => how_many) end def Mediaitem.recent_audio(how_many=24) Mediaitem.find_by_hasaudio_and_is_private( true, false , :order_by => :created_time, :order => :descending, :limit => how_many) end #hasimage is unreliable in the db def HasImage return false if self.HasVideo return false if self.HasAudio return true end def Mediaitem.recent_images(how_many=24) #images are those that aren't video or audio #hasimage flag is not rustworthy Mediaitem.find_by_hasaudio_hasvideo_and_is_private(false,false,false ,:order_by => :created_time,:order => :descending,:limit => how_many) end def create_group_url(group_name) return "#{Group.url_from_name(group_name)}/media/#{guid}" end def relative_url_from_root return "media/"+self.guid+"/show.html" end def url return "/media/"+self.guid+"/show.html" end def post_comment_url return "/procyon/post_media_comment_form?mediaitem_id=#{id.to_s}&mediaitem_guid=#{guid}&return_url=#{CGI.escape relative_url_from_root}&mediaitem_thumb_w=#{thumbfile.width}&mediaitem_thumb_h=#{thumbfile.height}&mediaitem_thumb_url=#{thumbfile.url}" end def thumbfile for mediafile in self.mediafiles if mediafile.width<=120 and mediafile.height<=120 and (mediafile.width==120 or mediafile.height==120) and mediafile.mimeType.index('image/')==0 return mediafile end end return tiny_thumbfile end def tiny_thumbfile for mediafile in self.mediafiles #not square if mediafile.width<=60 and mediafile.height<=60 and (mediafile.height==60 || mediafile.width==60) and mediafile.mimeType.index('image/')==0 return mediafile end end return nil end def square_thumbfile for mediafile in self.mediafiles if mediafile.width==100 and mediafile.height==100 and mediafile.mimeType.index('image/')==0 return mediafile end end return tiny_thumbfile end def tiny_square_thumbfile for mediafile in self.mediafiles if mediafile.width==50 and mediafile.height==50 and mediafile.mimeType.index('image/')==0 return mediafile end end return tiny_thumbfile end def big_thumbfile for mediafile in self.mediafiles if mediafile.width>120 and mediafile.width<=240 and mediafile.height>120 and mediafile.height<=240 and mediafile.mimeType.index('image/')==0 return mediafile end end return thumbfile end def file640 biggest=nil for mediafile in self.mediafiles if mediafile.width<=640 and mediafile.height<=640 and mediafile.width>1 if !biggest || mediafile.width>biggest.width biggest=mediafile end end end if !biggest return big_thumbfile end return biggest end def fileMaxSize result=nil for mediafile in self.mediafiles if result==nil || mediafile.width>result.width result=mediafile end end return result end def large_files result=[] for mediafile in self.mediafiles if mediafile.width>240 or mediafile.height>240 result<