Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Weibo client, add picture upload
- Loading branch information
Showing
8 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters