diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 0d108ba31..b6f1114bd 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,5 +1,6 @@ # -*- encoding : utf-8 -*- class Api::V1::BaseController < ApplicationController + include PaginateHelper #respond_to :json helper_method :member_path @@ -72,15 +73,6 @@ class Api::V1::BaseController < ApplicationController end end - def paginate_params - per_page = params[:per_page].to_i - per_page = 20 if per_page < 1 - per_page = 100 if per_page >100 - page = params[:page].to_i - page = nil if page == 0 - {:page => page, :per_page => per_page} - end - def render_json_response(subject, message, status = 200) id = status != 200 ? nil : subject.id diff --git a/app/controllers/platforms/contents_controller.rb b/app/controllers/platforms/contents_controller.rb new file mode 100644 index 000000000..d98c64324 --- /dev/null +++ b/app/controllers/platforms/contents_controller.rb @@ -0,0 +1,16 @@ +class Platforms::ContentsController < Platforms::BaseController + include PaginateHelper + + before_filter :authenticate_user! + skip_before_filter :authenticate_user! if APP_CONFIG['anonymous_access'] + + load_and_authorize_resource :platform + + def index + @path = '/' << params[:path].to_s + @term = params[:term] + @contents = PlatformContent.find_by_platform(@platform, @path, @term) + .paginate(paginate_params) + end + +end diff --git a/app/helpers/paginate_helper.rb b/app/helpers/paginate_helper.rb new file mode 100644 index 000000000..aa5d0e26f --- /dev/null +++ b/app/helpers/paginate_helper.rb @@ -0,0 +1,13 @@ +# -*- encoding : utf-8 -*- +module PaginateHelper + + def paginate_params + per_page = params[:per_page].to_i + per_page = 20 if per_page < 1 + per_page = 100 if per_page >100 + page = params[:page].to_i + page = nil if page == 0 + {:page => page, :per_page => per_page} + end + +end diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb new file mode 100644 index 000000000..f5dbca177 --- /dev/null +++ b/app/models/platform_content.rb @@ -0,0 +1,82 @@ +class PlatformContent + + # ------------------ + # *** ATTRIBUTES *** + # ------------------ + + attr_reader :path + + # --------------- + # *** METHODS *** + # --------------- + + def initialize(platform, path) + @platform, @path = platform, path + end + + def build_list + return @build_list if !!@build_list + return nil if path !~ /\/(release|updates)+\/\w/ + return nil unless repository_name = path.match(/\/[\w]+\/(release|updates)\//) + repository_name = repository_name[0].gsub(/\/(release|updates)\/$/, '').gsub('/', '') + + repository = @platform.repositories.where(:name => repository_name).first + return nil unless repository + + if @platform.main? + build_for_platform = @platform + else + bfp_name = path.match(/\/#{@platform.name}\/repository\/[\w]+\//) + return nil unless bfp_name + bfp_name = bfp_name[0].gsub(/\/#{@platform.name}\/repository\//, '').gsub('/', '') + build_for_platform = Platform.main.find_by_name bfp_name + return nil unless build_for_platform + end + + @build_list = BuildList.for_status(BuildList::BUILD_PUBLISHED) + .for_platform(build_for_platform) + .scoped_to_save_platform(@platform) + .where(:save_to_repository_id => repository) + .where(:build_list_packages => {:fullname => name, :actual => true}) + .joins(:packages) + .last + + return @build_list + end + + def name + @name ||= @path.gsub(/.*#{File::SEPARATOR}/, '') + end + + def size + @size ||= File.size(@path) + end + + def is_folder? + @is_folder.nil? ? (@is_folder = File.directory?(path)) : @is_folder + end + + def download_url + suffix = path.gsub(/^#{@platform.path}/, '') + "#{APP_CONFIG['downloads_url']}/#{@platform.name}#{suffix}" + end + + # --------------------- + # *** CLASS METHODS *** + # --------------------- + + def self.find_by_platform(platform, path, term) + term = (term.present? && term =~ /\w/) ? term : '' + path = path.split(File::SEPARATOR) + .select{ |p| p.present? && p =~ /\w/ } + .join(File::SEPARATOR) + results = Dir.glob(File.join(platform.path, path, "*#{term}*")) + if term + results = results.sort_by(&:length) + else + results = results.sort + end + results.map{ |p| PlatformContent.new(platform, p) } + end + +end \ No newline at end of file diff --git a/app/views/platforms/base/_sidebar.html.haml b/app/views/platforms/base/_sidebar.html.haml index 5775c9314..827837b02 100644 --- a/app/views/platforms/base/_sidebar.html.haml +++ b/app/views/platforms/base/_sidebar.html.haml @@ -10,6 +10,8 @@ = link_to t("layout.platforms.about"), platform_path(@platform) %li{:class => (contr == :repositories) ? 'active' : ''} = link_to t("layout.repositories.list_header"), platform_repositories_path(@platform) + %li{:class => (contr == :contents) ? 'active' : ''} + = link_to t('layout.platforms.contents'), platform_contents_path(@platform) - if can? :show, @platform %li{:class => (act == :index && contr == :maintainers) ? 'active' : nil} = link_to t("layout.platforms.maintainers"), platform_maintainers_path(@platform) diff --git a/app/views/platforms/contents/index.html.haml b/app/views/platforms/contents/index.html.haml new file mode 100644 index 000000000..40dfa4d48 --- /dev/null +++ b/app/views/platforms/contents/index.html.haml @@ -0,0 +1,30 @@ +- set_meta_tags :title => [title_object(@platform), t('layout.platforms.contents')] += render 'platforms/base/submenu' += render 'platforms/base/sidebar' + +%div + = "#{@platform.name}: #{@path}" + + +%table.tablesorter.project{:cellpadding => "0", :cellspacing => "0"} + %tbody + + - if @path != '/' + %tr + %td= link_to '../', "#{platform_contents_path(@platform)}#{@path}/../" + %td + %td + + - (@contents.select(&:is_folder?) | @contents).each do |content| + %tr + %td + - if content.is_folder? + - pic = 'folder.png' + - path = "#{platform_contents_path(@platform)}#{@path}/#{content.name}" + .pic= image_tag pic || 'code.png' + .name= link_to(content.name, path || content.download_url, :class => 'files-see') + %td= link_to t('activerecord.models.build_list'), content.build_list if content.build_list + %td= number_to_human_size(content.size) unless content.is_folder? +.both + += will_paginate @contents \ No newline at end of file diff --git a/app/views/platforms/platforms/show.html.haml b/app/views/platforms/platforms/show.html.haml index 0461c86d6..14aa01776 100644 --- a/app/views/platforms/platforms/show.html.haml +++ b/app/views/platforms/platforms/show.html.haml @@ -2,7 +2,9 @@ = render 'submenu' = render 'sidebar' -%h3.fix= "#{t("layout.platforms.about")} #{@platform.name}" +%h3.fix + = t 'layout.platforms.about' + = link_to @platform.name, platform_contents_path(@platform) %p= @platform.description diff --git a/config/routes.rb b/config/routes.rb index 5c5710fbf..1e3d9a442 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -161,6 +161,9 @@ Rosa::Application.routes.draw do get :advisories end + resources :contents, :only => [:index] + match '/contents/*path' => 'contents#index', :format => false + resources :mass_builds, :only => [:create, :new, :index] do member do post :cancel