Skip to content

Commit

Permalink
Add Weibo client, add picture upload
Browse files Browse the repository at this point in the history
  • Loading branch information
teeceepee committed Feb 12, 2017
1 parent 8f9dbcf commit 7543bef
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 1 deletion.
29 changes: 29 additions & 0 deletions 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
23 changes: 23 additions & 0 deletions 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
27 changes: 27 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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
7 changes: 7 additions & 0 deletions 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'
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -17,6 +17,7 @@
end
end
resources :sessions, only: [:create]
resources :pictures, only: [:index, :create]

namespace :pages do
get :girls
Expand Down
10 changes: 10 additions & 0 deletions 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
9 changes: 8 additions & 1 deletion db/schema.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 7543bef

Please sign in to comment.