fetch manifest.json at backend

pull/171/head
spirit 2019-12-15 16:57:53 +08:00
parent d91e1ad885
commit a99102fdac
9 changed files with 44 additions and 83 deletions

View File

@ -21,7 +21,7 @@ add_action('rest_api_init', function () {
'callback' => 'feature_gallery',
));
register_rest_route('sakura/v1', '/database/update', array(
'methods' => 'POST',
'methods' => 'GET',
'callback' => 'update_database',
));
register_rest_route('sakura/v1', '/qqinfo/json', array(
@ -367,48 +367,30 @@ function feature_gallery() {
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/database/update
*/
function update_database() {
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
$user = wp_authenticate($username, $password);
if (is_a($user, 'WP_User')) {
if (in_array('administrator', (array) $user->roles)) {
global $wpdb;
$sakura_table_name = $wpdb->base_prefix.'sakura';
if(isset($_FILES["manifest"])) {
$manifest = array(
"key" => "manifest_json",
"value" => file_get_contents($_FILES["manifest"]["tmp_name"])
);
$time = array(
"key" => "json_time",
"value" => date("Y-m-d H:i:s",time())
);
$wpdb->query("DELETE FROM `wp_sakura` WHERE `mate_key` ='manifest_json'");
$wpdb->query("DELETE FROM `wp_sakura` WHERE `mate_key` ='json_time'");
$wpdb->insert($sakura_table_name,$manifest);
$wpdb->insert($sakura_table_name,$time);
$message = "manifest.json has been stored into database.";
}
$output = array(
'status' => 200,
'success' => true,
'message' => $message
);
$result = new WP_REST_Response($output, 200);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}
} else {
$output = array(
'status' => 401,
'success' => false,
'message' => 'Not Authorized.'
global $wpdb;
$sakura_table_name = $wpdb->base_prefix.'sakura';
$img_domain = akina_option('cover_cdn') ? akina_option('cover_cdn') : get_template_directory_uri();
$manifest = file_get_contents($img_domain . "/manifest/manifest.json");
if($manifest) {
$manifest = array(
"key" => "manifest_json",
"value" => $manifest
);
$result = new WP_REST_Response($output, 401);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
$time = array(
"key" => "json_time",
"value" => date("Y-m-d H:i:s",time())
);
$wpdb->query("DELETE FROM `wp_sakura` WHERE `mate_key` ='manifest_json'");
$wpdb->query("DELETE FROM `wp_sakura` WHERE `mate_key` ='json_time'");
$wpdb->insert($sakura_table_name,$manifest);
$wpdb->insert($sakura_table_name,$time);
$output = "manifest.json has been stored into database.";
}else{
$output = "manifest.json not found, please ensure your url is corrent.";
}
$result = new WP_REST_Response($output, 200);
return $result;
}
/**

Binary file not shown.

View File

@ -1,8 +1,8 @@
msgid ""
msgstr ""
"Project-Id-Version: Sakura\n"
"POT-Creation-Date: 2019-12-13 14:51+0800\n"
"PO-Revision-Date: 2019-12-13 14:51+0800\n"
"POT-Creation-Date: 2019-12-15 14:40+0800\n"
"PO-Revision-Date: 2019-12-15 14:40+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: en_US\n"
@ -1307,11 +1307,12 @@ msgid "Cover CDN"
msgstr ""
#: options.php:880
#, php-format
msgid ""
"Fill in the cdn path for random picture display, without adding a slash at "
"the end, for example: https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3, "
"please refer to <a href = \"https: //github.com/mashirozx/Sakura/wiki/options"
"\">Wiki </a>"
"\">Wiki </a>. Click <a href = \"%s\">here</a> to update manifest"
msgstr ""
#: options.php:886

View File

@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
"Project-Id-Version: Sakura\n"
"POT-Creation-Date: 2019-12-13 14:50+0800\n"
"POT-Creation-Date: 2019-12-15 14:40+0800\n"
"PO-Revision-Date: 2019-11-01 14:27+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
@ -1291,10 +1291,12 @@ msgid "Cover CDN"
msgstr ""
#: options.php:880
#, php-format
msgid ""
"Fill in the cdn path for random picture display, without adding a slash at the end, for "
"example: https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3, please refer to <a href = "
"\"https: //github.com/mashirozx/Sakura/wiki/options\">Wiki </a>"
"\"https: //github.com/mashirozx/Sakura/wiki/options\">Wiki </a>. Click <a href = \"%s\">here</"
"a> to update manifest"
msgstr ""
#: options.php:886

Binary file not shown.

View File

@ -1,8 +1,8 @@
msgid ""
msgstr ""
"Project-Id-Version: Sakura\n"
"POT-Creation-Date: 2019-12-13 14:51+0800\n"
"PO-Revision-Date: 2019-12-13 14:51+0800\n"
"POT-Creation-Date: 2019-12-15 14:41+0800\n"
"PO-Revision-Date: 2019-12-15 14:41+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: zh_CN\n"
@ -1328,15 +1328,23 @@ msgid "Cover CDN"
msgstr "封面图图片库"
#: options.php:880
#, fuzzy, php-format
#| msgid ""
#| "Fill in the cdn path for random picture display, without adding a slash "
#| "at the end, for example: https://cdn.jsdelivr.net/gh/mashirozx/"
#| "sakura@3.3.3, please refer to <a href = \"https: //github.com/mashirozx/"
#| "Sakura/wiki/options\">Wiki </a>. Click <a href = \"%s\">here</> to update "
#| "manifest"
msgid ""
"Fill in the cdn path for random picture display, without adding a slash at "
"the end, for example: https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3, "
"please refer to <a href = \"https: //github.com/mashirozx/Sakura/wiki/options"
"\">Wiki </a>"
"\">Wiki </a>. Click <a href = \"%s\">here</a> to update manifest"
msgstr ""
"填写 cdn 路径用于随机图片展示最后不用加斜杠https://cdn.jsdelivr."
"net/gh/mashirozx/sakura@3.3.3 ,更多信息请参考<a href=\"https://github.com/"
"mashirozx/Sakura/wiki/options\">Wiki</a>"
"mashirozx/Sakura/wiki/options\">Wiki</a>。点击<a href =\"%s\">这里</a>更新"
"manifest"
#: options.php:886
msgid "Use the front-end library locally (lib.js、lib.css)"

View File

@ -24,8 +24,6 @@ pip3 install Pillow
pip install Pillow --user
```
用同样的方法安装`requests`
### 运行
把图片文件放到 `gallary` 目录Windows 可直接双击 manifest.py或者和其他操作系统一样在 Terminal、Powershell、CMD 中运行:

View File

@ -7,8 +7,6 @@ Desc: Webp convertor
import os
import sys
import json
import requests
import base64
import hashlib
from PIL import Image
@ -50,29 +48,6 @@ class Single(object):
self.manifest()
return self.mani
class Upload2Wordpress(object):
def __init__(self, username, password, url):
self.username = username
self.password = password
self.url = url
def upload(self, file, field):
data_string = self.username + ':' + self.password
token = base64.b64encode(data_string.encode()).decode('utf-8')
headers = {
'Authorization': 'Basic ' + token,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97"
}
files = {field: open(file, mode="rb")}
reply = requests.post(self.url, headers=headers, files=files)
print(json.loads(reply.content)['message'])
def main(self):
print('start uploading `manifest.json`...')
self.upload('manifest.json', 'manifest')
def gen_manifest_json():
onlyfiles = [f for f in os.listdir('gallary') if os.path.isfile(os.path.join('gallary', f))]
id = 1
@ -88,11 +63,6 @@ def gen_manifest_json():
def main():
gen_manifest_json()
username = input('Enter your username: ')
password = input('Enter your password: ')
url = input('Enter your rest api url: ')
upload = Upload2Wordpress(username, password, url)
upload.main()
if __name__ == '__main__':

View File

@ -877,7 +877,7 @@ function optionsframework_options() {
$options[] = array(
'name' => __('Cover CDN', 'sakura'),/*封面图片库*/
'desc' => __('Fill in the cdn path for random picture display, without adding a slash at the end, for example: https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3, please refer to <a href = "https: //github.com/mashirozx/Sakura/wiki/options">Wiki </a>', 'sakura'),/*填写 cdn 路径用于随机图片展示最后不用加斜杠https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3 ,更多信息请参考<a href="https://github.com/mashirozx/Sakura/wiki/options">Wiki</a>*/
'desc' => sprintf(__('Fill in the cdn path for random picture display, without adding a slash at the end, for example: https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3, please refer to <a href = "https: //github.com/mashirozx/Sakura/wiki/options">Wiki </a>. Click <a href = "%s">here</a> to update manifest', 'sakura'), rest_url('sakura/v1/database/update')),/*填写 cdn 路径用于随机图片展示最后不用加斜杠https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3 ,更多信息请参考<a href="https://github.com/mashirozx/Sakura/wiki/options">Wiki</a>*/
'id' => 'cover_cdn',
'std' => 'https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3',
'type' => 'text');