From 7543befa2f33fab998245e46107f9b360e450e67 Mon Sep 17 00:00:00 2001 From: Yu Haidong Date: Sun, 12 Feb 2017 23:33:38 +0800 Subject: [PATCH] Add Weibo client, add picture upload --- app/clients/weibo.rb | 29 ++++++++++++++++++++ app/controllers/pictures_controller.rb | 23 ++++++++++++++++ app/models/picture.rb | 27 ++++++++++++++++++ app/views/pictures/create.html.slim | 8 ++++++ app/views/pictures/index.html.slim | 7 +++++ config/routes.rb | 1 + db/migrate/20170212142609_create_pictures.rb | 10 +++++++ db/schema.rb | 9 +++++- 8 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 app/clients/weibo.rb create mode 100644 app/controllers/pictures_controller.rb create mode 100644 app/models/picture.rb create mode 100644 app/views/pictures/create.html.slim create mode 100644 app/views/pictures/index.html.slim create mode 100644 db/migrate/20170212142609_create_pictures.rb diff --git a/app/clients/weibo.rb b/app/clients/weibo.rb new file mode 100644 index 0000000..28a6321 --- /dev/null +++ b/app/clients/weibo.rb @@ -0,0 +1,29 @@ +class Weibo + UPLOAD_URL = 'http://picupload.service.weibo.com/interface/pic_upload.php?' + + 'cb=http%3A%2F%2Fwww.weibo.com%2F&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog&s=rdxt' + COOKIE_STRING_KEY = 'weibo:cookie_string' + + def initialize + end + + def upload_picture(file, content_type) + form_file = HTTP::FormData::File.new(file, mime_type: content_type) + resp = HTTP.headers(cookie: cookie_string).post(UPLOAD_URL, form: {pic1: form_file}) + + redirect_url = URI(resp.headers['Location'] || '') + query = Rack::Utils.parse_query(redirect_url.query) + query['pid'] + end + + def cookie_string + @cookie_string ||= self.class.get_cookie_string + end + + def self.set_cookie_string(cookie_string) + Redis.current.set(COOKIE_STRING_KEY, cookie_string) + end + + def self.get_cookie_string + Redis.current.get(COOKIE_STRING_KEY) + end +end diff --git a/app/controllers/pictures_controller.rb b/app/controllers/pictures_controller.rb new file mode 100644 index 0000000..9af33eb --- /dev/null +++ b/app/controllers/pictures_controller.rb @@ -0,0 +1,23 @@ +class PicturesController < ApplicationController + + def index + + end + + def create + if params[:picture].present? + @picture = Picture.new(picture_params) + @picture.upload_picture + @picture.save + else + redirect_to :back + end + end + + private + + def picture_params + params.require(:picture).permit(:file) + end + +end diff --git a/app/models/picture.rb b/app/models/picture.rb new file mode 100644 index 0000000..cc73f21 --- /dev/null +++ b/app/models/picture.rb @@ -0,0 +1,27 @@ +# Sina CDN domains: wx1.sinaimg.cn wx2.sinaimg.cn wx3.sinaimg.cn wx4.sinaimg.cn +# Sina CDN versions: square large +class Picture < ApplicationRecord + attr_accessor :file + + ## validations + validates_presence_of :filename, :pid + + def sinaimg_url(version = 'large') + "https://wx1.sinaimg.cn/#{version}/#{self.pid}#{self.extname}" + end + + def extname + if File.extname(self.filename) == '.gif' + '.gif' + else + '.jpg' + end + end + + def upload_picture + if self.file.present? + self.filename = self.file.original_filename + self.pid = Weibo.new.upload_picture(self.file.tempfile, self.file.content_type) + end + end +end diff --git a/app/views/pictures/create.html.slim b/app/views/pictures/create.html.slim new file mode 100644 index 0000000..b69c1e4 --- /dev/null +++ b/app/views/pictures/create.html.slim @@ -0,0 +1,8 @@ +.row + .col-sm-6.col-sm-offset-3 + - if @picture.persisted? + h1 Uploaded Picture + .thumbnail + = image_tag(@picture.sinaimg_url) + - else + h1 Upload Failed diff --git a/app/views/pictures/index.html.slim b/app/views/pictures/index.html.slim new file mode 100644 index 0000000..f124a1e --- /dev/null +++ b/app/views/pictures/index.html.slim @@ -0,0 +1,7 @@ +.row + .col-sm-6.col-sm-offset-3 + h1 Select Picture + .well + = simple_form_for :picture, url: pictures_path do |f| + = f.input :file, as: :file + = f.submit class: 'btn btn-default btn-block' diff --git a/config/routes.rb b/config/routes.rb index fdb57e3..fcd41bc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,7 @@ end end resources :sessions, only: [:create] + resources :pictures, only: [:index, :create] namespace :pages do get :girls diff --git a/db/migrate/20170212142609_create_pictures.rb b/db/migrate/20170212142609_create_pictures.rb new file mode 100644 index 0000000..b9a6fdc --- /dev/null +++ b/db/migrate/20170212142609_create_pictures.rb @@ -0,0 +1,10 @@ +class CreatePictures < ActiveRecord::Migration[5.0] + def change + create_table :pictures do |t| + t.string :filename + t.string :pid + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index be67f64..a1903f9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161223163447) do +ActiveRecord::Schema.define(version: 20170212142609) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -46,6 +46,13 @@ t.datetime "updated_at", null: false end + create_table "pictures", force: :cascade do |t| + t.string "filename" + t.string "pid" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "positions", force: :cascade do |t| t.jsonb "json" t.jsonb "box"