Guest User

Untitled

a guest
Jun 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. <---app/models/podcast.rb--->
  2.  
  3. # A video uploaded into the site and converted on the server. The actual conversion happens
  4. # in the VideoConversionWorker found in lib/workers/video_conversion_worker.rb
  5. #
  6. # Currently videos are automatically converted into FLV and OGG formats, and the original
  7. # video is also saved on the server. A torrent file is also automatically created so that
  8. # the video can be shared by site users.
  9. #
  10. class Podcast < ActiveRecord::Base
  11.  
  12.  
  13. WEB_ROOT = 'system/'
  14.  
  15. # Macros
  16. #
  17. upload_column :file,
  18. :extensions => ["mp3", "wav", "ogg",],
  19. :root_dir => File.join(RAILS_ROOT, "public", "system"),
  20. :web_root => "/system",
  21. :store_dir => proc {|record, file|
  22. if !record.created_on.nil?
  23. return record.created_on.strftime("video/%Y/%m/%d/") + record.id.to_s
  24. else
  25. return Date.today.strftime("video/%Y/%m/%d/") + record.id.to_s
  26. end
  27. }
  28.  
  29. # Validations
  30. #
  31. validates_length_of :title, :maximum=>255
  32. validates_presence_of :file unless RAILS_ENV == 'test'
  33.  
  34. # Assocations
  35. #
  36. belongs_to :post, :foreign_key => "content_id"
  37.  
  38. # Filters
  39. #
  40. before_destroy :delete_files
  41. before_update :delete_files_if_new_uploaded
  42.  
  43. # Accessors
  44. #
  45. attr_accessor :video_type, :relative_video_file, :relative_ogg_file, :relative_torrent_file
  46.  
  47. PROCESSING = 1
  48. SUCCESS = 2
  49. #ERROR = 3 # not used currently as I'm not sure how to trap errors.
  50.  
  51. # Recursively deletes all files and then the directory which the files
  52. # were stored in.
  53. #
  54. def delete_files
  55. FileUtils.remove_dir(file.store_dir)
  56. end
  57.  
  58.  
  59. # Recursively deletes all files and then the directory which the files
  60. # were stored in, if a new file was uploaded during this request.
  61. #
  62. def delete_files_if_new_uploaded
  63. if self.file.new_file?
  64. FileUtils.remove_dir(file.store_dir)
  65. end
  66. end
  67.  
  68. # TODO: I'd like to have the validates_file_format_of use this array but it
  69. # returns method_missing for some reason I can't fathom. It is being used in the
  70. # views already.
  71. #
  72. def self.allowed_file_types
  73. ["3gp", "avi", "m4v", "mov", "mpg", "mpeg", "mp4", "ogg", "wmv"]
  74. end
  75.  
  76. # Gets an absolute path to this video's file and send it to the
  77. # VideoConversionWorker for conversion.
  78. #
  79. def convert
  80. unless RAILS_ENV == 'test'
  81. video_file_to_convert = File.expand_path(RAILS_ROOT) + "/public/" + self.file.url
  82. MiddleMan.new_worker(:class =>
  83. :video_conversion_worker,
  84. :job_key => "video"+self.object_id.to_s,
  85. :args => {
  86. :absolute_path => video_file_to_convert,
  87. :torrent_tracker => Hyperactive.torrent_tracker,
  88. :video_id => self.id.to_s }) unless RAILS_ENV == 'test'
  89. end
  90. end
  91.  
  92. # Currently this always return "ogg" since we only use it for inclusion in the
  93. # ogg-only video feed.
  94. #
  95. def video_type
  96. 'application/ogg'
  97. end
  98.  
  99. # The relative path to the video file.
  100. #
  101. def relative_video_file
  102. self.file.url
  103. end
  104.  
  105. # The relative path to the converted ogg file for this video.
  106. #
  107. def relative_ogg_file
  108. self.file.url + ".ogg"
  109. end
  110.  
  111. # The thumbnail for this video.
  112. #
  113. def thumbnail
  114. self.file.url + ".jpg"
  115. end
  116.  
  117. # The relative path to the torrent file for this video.
  118. #
  119. def relative_torrent_file
  120. relative_ogg_file + ".torrent"
  121. end
  122.  
  123. # A convenience method telling us whether this content has a thumbnail
  124. # which we can use in a view. This is inherited from Content, and is
  125. # overridden here so that it's always true, since all video objects
  126. # should have a thumbnail.
  127. #
  128. def has_thumbnail?
  129. true
  130. end
  131.  
  132. # A convenience method returning the content object that this content
  133. # is attached to. Currently this should only work for Video.
  134. # We could just use "self.content" but this method makes
  135. # things hopefully a little more clear as to what's going on.
  136. #
  137. def related_content
  138. self.content
  139. end
  140.  
  141. end
  142.  
  143. <--app/controller/podcast_controller.rb-->
  144.  
  145. # POST /podcasts
  146. # POST /podcasts.xml
  147. def create
  148. @podcast = Podcast.new(params[:podcast])
  149.  
  150. respond_to do |format|
  151. if @podcast.save
  152. flash[:notice] = 'Podcast was successfully created.'
  153. format.html { redirect_to(@podcast) }
  154. format.xml { render :xml => @podcast, :status => :created, :location => @podcast }
  155. else
  156. format.html { render :action => "new" }
  157. format.xml { render :xml => @podcast.errors, :status => :unprocessable_entity }
  158. end
  159. end
  160. end
Add Comment
Please, Sign In to add comment