Permalink
Browse files

AJAX direct upload support

Now you can let file upload to TieTuKu server directly, not transfer from qchan-lite.
  • Loading branch information...
1 parent 8b0c3f3 commit c2cf680a9c21476009f1883fd894044713f2d3b8 qakcn committed Sep 12, 2014
View
@@ -12,8 +12,8 @@ A light-weight image upload and manage program for other image host service.
qchan-lite
==========
-由于其他图床服务的轻量级图片上传和管理程序
+用于其他图床服务的轻量级图片上传和管理程序
## 支持的服务
-[贴图库](http://tietuku.com)
+[贴图库](http://tietuku.com)
View
@@ -8,7 +8,11 @@
exit('API cannot be direct accessed!');
}
if(check_apikey()) {
- if(isset($_GET['type']) && $_GET['type']=='url') {
+ if(isset($_GET['gettoken']) && $_GET['gettoken']=='file') {
+ $result = get_token('file');
+ }else if(isset($_GET['gettoken']) && $_GET['gettoken']=='url') {
+ $result = get_token('url');
+ }else if(isset($_GET['type']) && $_GET['type']=='url') {
$result = url_handler();
}else if(isset($_GET['type']) && $_GET['type']=='file') {
$result = array_pop(file_handler());
View
@@ -20,6 +20,7 @@
// 服务设置
define('SERVICE', 'tietuku'); // 服务提供商:目前仅支持'tietuku'(贴图库)
define('ALBUM_PREFIX', 'qchan'); // 相册前缀
+define('DIRECT_AJAX', false); // 设置为true时直接上传到服务提供商,不经过本程序的服务器
// 贴图库API设置
// 下面两个Key请到[贴图库]->[开放平台]->[管理中心]->[密钥]获取
View
@@ -67,7 +67,7 @@ function check_config() {
}
}
-define('QCHAN_VER', 'lite-1.0pre build 20140905');
+define('QCHAN_VER', 'lite-1.0pre build 20140912');
date_default_timezone_set('UTC');
define('ABSPATH', __DIR__);
@@ -22,6 +22,12 @@ function load_header() {
}
function load_footer() {
+ if(defined('DIRECT_AJAX') && DIRECT_AJAX) {
+ if(SERVICE=='tietuku') {
+ echo '<script type="application/javascript" src="'.get_url().theme_path().'ttk-direct-ajax.js"></script>';
+ }
+
+ }
require_once ABSPATH.'/'.theme_path().'footer.php';
}
@@ -2,14 +2,19 @@
// Setup path for upload
function set_album() {
- $album_name=Service::srv()->formatAlbubumName();
+ $album_name=Service::srv()->formatAlbumName();
$id = Service::srv()->getAlbumByName($album_name);
if($id===false) {
$id = Service::srv()->createAlbum($album_name);
}
return $id;
}
+function get_token($method) {
+ $aid = set_album();
+ return array('token'=>Service::srv()->getToken($aid, $method));
+}
+
function set_pic_size($width_orig, $height_orig) {
$height = 200;
$width = 1000;
@@ -33,7 +38,7 @@ function url_handler() {
$aid = set_album();
$url = $_POST['url'];
$qid = $_POST['qid'];
- if(preg_match('/^\s*https?:\/\/.+$/', $url)) {
+ if(!preg_match('/^\s*https?:\/\/.+$/', $url)) {
return array('status' => 'failed', 'err' => 'wrong_type');
}
$pic = Service::srv()->uploadPicByURL($aid, $url);
@@ -1,7 +1,7 @@
<?php
class MyTieTuKu extends TTKClient{
- function uploadFile($aid,$file,$filename) {
+ function uploadFile($aid,$file=null,$filename='') {
$url = $this->upload_host;
$param['deadline'] = time()+$this->timeout;
$param['aid'] = $aid;
@@ -22,6 +22,14 @@ public function formatAlbumName($year=null, $month=null) {
}
}
+ public function getToken($aid, $method) {
+ if($method=='file') {
+ return $this->tietuku->uploadFile($aid);
+ }else if($method=='url') {
+ return $this->tietuku->uploadFromWeb($aid);
+ }
+ }
+
//上传文件
public function uploadPicByFile($aid, $file,$filename=null) {
if(is_null($filename)) {
@@ -51,7 +59,7 @@ public function uploadPicByURL($aid, $url) {
'show_url' => $al['s_url'],
'width' => (int)$al['width'],
'height' => (int)$al['height'],
- 'name' => basename($file)
+ 'name' => $url
);
}else {
return false;
@@ -514,7 +514,7 @@ function uploadFromWeb($aid,$fileurl=null){
$Token=$this->op_Token->dealParam($param)->createToken();
$data['Token']=$Token;
$data['fileurl']=$fileurl;
- return empty($url)?$Token:$this->post($url,$data);
+ return empty($fileurl)?$Token:$this->post($url,$data);
}
/**
* 对接口post数据
@@ -0,0 +1,121 @@
+/* 贴图库直接上传的JS文件 */
+
+
+
+
+function ttk_get_token(method,callback) {
+ var xhr = new XMLHttpRequest();
+
+ xhr.open('GET', 'api.php?gettoken='+method, true);
+
+ xhr.addEventListener('readystatechange', (function(callback){
+ return function(e) {
+ if(xhr.readyState == 4) {
+ if(xhr.status == 200) {
+ var res=JSON.parse(xhr.responseText);
+ callback(res.token);
+ }
+ }
+ }
+ })(callback),false);
+
+ xhr.send();
+}
+
+function set_pic_size(width_orig, height_orig) {
+ height = 200;
+ width = 1000;
+ re={};
+ if(height_orig <= height && width_orig <= width) {
+ re.width = width_orig;
+ re.height = height_orig;
+ }else{
+ ratio_orig = width_orig/height_orig;
+ if (width/height > ratio_orig) {
+ width = height*ratio_orig;
+ }else {
+ height = width/ratio_orig;
+ }
+ re.width = width;
+ re.height = height;
+ }
+ return re;
+}
+
+function ttk_upload(work,self,method) {
+ ttk_get_token(method, (function(work,self){
+ return function(ttktoken){
+ var xhr = new XMLHttpRequest();
+ var fd = new FormData();
+
+ xhr.open('POST', 'http://up.tietuku.com/', true);
+ //xhr.setRequestHeader("Content-type", "multipart/form-data");
+
+ xhr.addEventListener('readystatechange', (function(qid){
+ return function(e) {
+ if(xhr.readyState == 4) {
+ if(xhr.status == 200) {
+ var res=JSON.parse(xhr.responseText);
+ var pic_size=set_pic_size(res.width, res.height);
+ if(!res.code)
+ fin= {
+ 'qid': qid,
+ 'status': 'success',
+ 'path': res.linkurl,
+ 'thumb': res.t_url,
+ 'name': res.name,
+ 'width': pic_size.width,
+ 'height': pic_size.height
+ };
+ after_upload(fin);
+ upload_next();
+ }
+ }
+ }
+ })(work.qid),false);
+
+ xhr.upload.addEventListener('progress',function(e){
+ if(e.lengthComputable) {
+ var percentage = e.loaded/e.total;
+ self.progress(percentage);
+ }
+ },false);
+ xhr.upload.addEventListener('load',function(e){
+ self.progress(1);
+ },false);
+
+ fd.append('Token',ttktoken);
+ if(method == 'file') {
+ fd.append('file',work.fileobj);
+ }else if(method == 'url') {
+ fd.append('fileurl',work.path);
+ }
+
+ xhr.send(fd);
+ }
+ })(work,self));
+}
+
+orig_url_upload=url_upload;
+url_upload=function(work) {
+ var self = document.getElementById('q'+work.qid);
+ if(!self) {
+ var callself = function(){url_upload(work);};
+ setTimeout(callself,100);
+ return false;
+ }else {
+ ttk_upload(work,self,'url');
+ }
+}
+
+orig_file_upload=file_upload;
+file_upload=function(work) {
+ var self = document.getElementById('q'+work.qid);
+ if(!self) {
+ var callself = function(){file_upload(work);};
+ setTimeout(callself,100);
+ return false;
+ }else {
+ ttk_upload(work,self,'file');
+ }
+}
@@ -163,7 +163,7 @@ function file_upload(work) {
xhr.addEventListener('readystatechange', function(e){
if(xhr.readyState == 4) {
if(xhr.status == 200) {
- eval('var res = '+xhr.responseText);
+ res=JSON.parse(xhr.responseText);
after_upload(res);
upload_next();
}

0 comments on commit c2cf680

Please sign in to comment.