diff --git a/README.md b/README.md index 2b62be62..0d1008ad 100644 --- a/README.md +++ b/README.md @@ -1363,6 +1363,65 @@ http://localhost:3000/api/projects | jq ``` --- +#### 推荐项目 +``` +GET api/projects/recommend +``` +*示例* +``` +curl -X GET \ +http://localhost:3000/api/projects/recommend | jq +``` + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|total_count |int |项目总条数 | +|id |string |项目id | +|name |string|项目名称| +|description |string|项目简介| +|visits |int|流量数| +|forked_count |int|被fork的数量| +|praises_count |int|star数量| +|is_public |boolean|是否公开, true:公开,false:未公开| +|mirror_url |string|镜像url| +|last_update_time|int|最后更新时间,为UNIX格式的时间戳| +|author |object|项目创建者| +|-- name |string|用户名,也是用户标识| +|category |object|项目类别| +|-- id |int|项目类型id| +|-- name |string|项目类型名称| +|language |object|项目语言| +|-- id |int|项目语言id| +|-- name |string|项目语言名称| + + +返回值 +``` +[ + { + "id": 20, + "repo_id": null, + "identifier": "PNAekinmH", + "name": "FNILL", + "visits": 13567, + "author": { + "name": "王一达", + "login": "wangyida", + "image_url": "avatars/User/b" + }, + "category": { + "id": 8, + "name": "其他" + } + }, + ... +] + +``` +--- + ### 获取分支列表 ``` GET /api/:namespace_id/:id/branches diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index a79aae42..16842fb5 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -2,7 +2,7 @@ # # 文件上传 class AttachmentsController < ApplicationController - before_action :require_login, :check_auth, except: [:show, :preview_attachment] + before_action :require_login, :check_auth, except: [:show, :preview_attachment, :get_file] before_action :find_file, only: %i[show destroy] before_action :attachment_candown, only: [:show] skip_before_action :check_sign, only: [:show, :create] @@ -28,6 +28,15 @@ class AttachmentsController < ApplicationController update_downloads(@file) end + + def get_file + normal_status(-1, "参数缺失") if params[:download_url].blank? + url = URI.encode(params[:download_url].to_s.gsub("http:", "https:")) + response = Faraday.get(url) + filename = params[:download_url].to_s.split("/").pop() + send_data(response.body.force_encoding("UTF-8"), filename: filename, type: "application/octet-stream", disposition: 'attachment') + end + def create # 1. 本地存储 # 2. 上传到云 diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index cadedcea..cee53128 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -2,9 +2,8 @@ class ProjectsController < ApplicationController include ApplicationHelper include OperateProjectAbilityAble include ProjectsHelper - - before_action :require_login, except: %i[index branches group_type_list simple show fork_users praise_users watch_users] - before_action :load_project, except: %i[index group_type_list migrate create] + before_action :require_login, except: %i[index branches group_type_list simple show fork_users praise_users watch_users recommend] + before_action :load_project, except: %i[index group_type_list migrate create recommend] before_action :authorizate_user_can_edit_project!, only: %i[update] before_action :project_public?, only: %i[fork_users praise_users watch_users] @@ -104,6 +103,10 @@ class ProjectsController < ApplicationController json_response(@project, current_user) end + def recommend + @projects = Project.recommend.includes(:repository, :project_category, owner: :user_extension).limit(5) + end + private def project_params diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 2b6d9d40..f41fbd36 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -1,11 +1,11 @@ module RepositoriesHelper def render_decode64_content(str) return nil if str.blank? - Base64.decode64(str).force_encoding('UTF-8') + Base64.decode64(str).force_encoding("UTF-8") end def download_type(str) - default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb png jpg gif tif psd svg RData rdata) + default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb png jpg gif tif psd svg RData rdata doc docx mpp vsdx) default_type.include?(str&.downcase) end diff --git a/app/models/project.rb b/app/models/project.rb index c30f67f1..42f35bfd 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -36,6 +36,8 @@ class Project < ApplicationRecord after_save :check_project_members scope :project_statics_select, -> {select(:id,:name, :is_public, :identifier, :status, :project_type, :user_id, :forked_count, :visits, :project_category_id, :project_language_id, :license_id, :ignore_id, :watchers_count, :created_on)} scope :no_anomory_projects, -> {where("projects.user_id is not null and projects.user_id != ?", 2)} + scope :recommend, -> { visible.project_statics_select.where(recommend: true) } + def self.search_project(search) diff --git a/app/views/projects/recommend.json.jbuilder b/app/views/projects/recommend.json.jbuilder new file mode 100644 index 00000000..1da5be40 --- /dev/null +++ b/app/views/projects/recommend.json.jbuilder @@ -0,0 +1,22 @@ +json.array! @projects do |project| + owner = project.owner + json.id project.id + json.repo_id project&.repository&.id + json.identifier project.identifier + json.name project.name + json.visits project.visits + json.author do + json.name owner.try(:show_real_name) + json.login owner.login + json.image_url url_to_avatar(owner) + end + + json.category do + if project.project_category.blank? + json.nil! + else + json.id project.project_category.id + json.name project.project_category.name + end + end +end diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index 4890f404..be603d91 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -7,7 +7,7 @@ json.sha entry['sha'] json.path entry['path'] json.type entry['type'] json.size entry['size'] -json.content entry['content'].present? && !direct_download ? render_decode64_content(entry['content']).force_encoding('UTF-8') : "" +json.content entry['content'].present? && !direct_download ? render_decode64_content(entry['content']) : "" json.target entry['target'] json.download_url entry['download_url'] json.direct_download direct_download diff --git a/config/routes.rb b/config/routes.rb index 7bbda653..d68fe8f2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,8 +8,10 @@ Rails.application.routes.draw do # Serve websocket cable requests in-process mount ActionCable.server => '/cable' + get 'attachments/entries/get_file', to: 'attachments#get_file' get 'attachments/download/:id', to: 'attachments#show' get 'attachments/download/:id/:filename', to: 'attachments#show' + get 'auth/qq/callback', to: 'oauth/qq#create' get 'auth/failure', to: 'oauth/base#auth_failure' get 'auth/cas/callback', to: 'oauth/cas#create' @@ -18,6 +20,8 @@ Rails.application.routes.draw do get 'oauth/register', to: 'oauth#register' post 'oauth/auto_register', to: 'oauth#auto_register' + resources :edu_settings + resources :edu_settings scope '/api' do namespace :ci do @@ -48,7 +52,7 @@ Rails.application.routes.draw do resources :compose_projects, only: [:create, :destroy] end resources :attachments do - member do + member do post :preview_attachment end collection do @@ -104,6 +108,7 @@ Rails.application.routes.draw do collection do post :migrate get :group_type_list + get :recommend end end diff --git a/db/migrate/20200929062837_add_index_to_users_type.rb b/db/migrate/20200929062837_add_index_to_users_type.rb new file mode 100644 index 00000000..d27ca9dd --- /dev/null +++ b/db/migrate/20200929062837_add_index_to_users_type.rb @@ -0,0 +1,5 @@ +class AddIndexToUsersType < ActiveRecord::Migration[5.2] + def change + add_index :users, :type + end +end diff --git a/db/migrate/20201004034434_add_recommend_to_projects.rb b/db/migrate/20201004034434_add_recommend_to_projects.rb new file mode 100644 index 00000000..d00b3eb6 --- /dev/null +++ b/db/migrate/20201004034434_add_recommend_to_projects.rb @@ -0,0 +1,5 @@ +class AddRecommendToProjects < ActiveRecord::Migration[5.2] + def change + add_column :projects, :recommend, :boolean, default: false + end +end