Skip to content

Commit

Permalink
add fontsync pull and push
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Mar 20, 2016
1 parent 0435331 commit f11da19
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 67 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ sh build.sh
23. 2016-2-25 增加字形导出svg和png功能.

24. 2016-3-7 将path的布尔操作库替换成paper.js的路径库.

25. 2016-3-20 增加字体同步的拉取和推送,增加自动更新同步版本.
3 changes: 2 additions & 1 deletion build/i18n.en-us.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ module.exports = exports = {
help: 'Help',
confirm: 'Confirm',
cancel: 'Cancel',
fontinfo: 'Font Info'
fontinfo: 'Font Info',
syncfromserver: 'Sync Font From Server'
};
3 changes: 2 additions & 1 deletion build/i18n.zh-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ module.exports = exports = {
help: '帮助',
confirm: '确定',
cancel: '取消',
fontinfo: '字体信息'
fontinfo: '字体信息',
syncfromserver: '从服务器同步字体'
};
77 changes: 47 additions & 30 deletions demo/sync/font.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* "fontName": "fonteditor", // 字体名称
* "hasNew": 1, // 如果有新数据则标记为1, 同时设置fontType, timestamp, ttf字段
* "timestamp": 12345678, // 新纪录时间戳,unix timestamp 精确到毫秒
* "fontType": "ttf", // 新纪录类型
* "fontType": "ttf", // 新纪录类型,默认为ttf字体
* "ttf": base64str // 新纪录的base64字体数据
* }
* }
Expand Down Expand Up @@ -54,8 +54,10 @@
*
* @author mengke01(kekee000@gmail.com)
*/
error_reporting(E_ALL ^ E_NOTICE);

define('SYNC_FILE', __DIR__ . '/list.md'); // 同步的文件
define('FONT_ROOT', __DIR__); // 同步字体的基础目录
define('SYNC_FILE', FONT_ROOT . '/list.md'); // 同步的文件



Expand All @@ -69,9 +71,13 @@
function jsonp($status, $data = null, $statusInfo = null) {
$callback = $_GET['callback'];
$json = array(
'status' => $status,
'data' => $data,
'status' => $status
);

if (!empty($data)) {
$json['data'] = $data;
}

if (!empty($statusInfo)) {
$json['statusInfo'] = $statusInfo;
}
Expand All @@ -85,7 +91,14 @@ function jsonp($status, $data = null, $statusInfo = null) {
* @return number
*/
function getTimestamp() {
return intval(microtime(true) * 1000);
return floor(microtime(true) * 1000);
}

function getFontPath() {
// 根据具体情况设置fontpath
// $fontpath = FONT_ROOT . dirname($_SERVER["REQUEST_URI"]);
// return preg_replace('/\\\+/', '/', $fontpath);
return FONT_ROOT;
}

/**
Expand Down Expand Up @@ -134,6 +147,8 @@ function getSyncRecord() {

/**
* 保存同步的记录
*
* @param array $data 同步的记录列表
*/
function saveSyncRecord($data) {
file_put_contents(SYNC_FILE, json_encode($data));
Expand All @@ -152,17 +167,18 @@ function doPush() {

$ret = array(); // 记录成功的类型

$fontPath = getFontPath();
foreach (explode(',', $fontType) as $type) {
if (!empty($_POST[$type])) {
writeBase64File($_POST[$type], "${fontName}.${type}");
writeBase64File($_POST[$type], "${fontPath}/${fontName}.${type}");
$ret[] = $type;
}
}

// 保存同步记录
$timestamp = getTimestamp();
$recordList = getSyncRecord();
$recordList[$fontName] = array(
$recordList["${fontPath}/${fontName}"] = array(
'user' => $_COOKIE['FONT_USER'],
'timestamp' => $timestamp,
'fontType' => $ret[0],
Expand All @@ -189,34 +205,35 @@ function doPush() {
*/
function doPull() {
$fontName = $_GET['fontName'];
$fontType = $_GET['fontType'];
$timestamp = empty($_GET['timestamp']) ? 0 : intval($_GET['timestamp']);
$fontType = $_GET['fontType']; // 默认拉取的为ttf字体
$timestamp = empty($_GET['timestamp']) ? 0 : floor($_GET['timestamp']);
if (empty($fontName)) {
jsonp(1, null, 'missing font field fontName!');
return;
}

$fontPath = getFontPath();
$recordList = getSyncRecord();
if (!empty($recordList[$fontName])) {
$record = $recordList[$fontName];
//var_dump($record);
// 最后一次提交不是当前用户,或者强制拉取
if (
($record['user'] != $_COOKIE['FONT_USER'] && $record['timestamp'] > $timestamp)
|| -1 === $timestamp
) {

$fontFile = "${fontName}.${fontType}";
if (file_exists($fontFile)) {
$data = array(
'fontName' => $fontName,
'hasNew' => 1,
'timestamp' => $record['timestamp'],
'fontType' => $fontType,
);
$data[$fontType] = readBase64File($fontFile);
jsonp(0, $data);
return;
}
if (!empty($recordList["${fontPath}/${fontName}"])) {
$record = $recordList["${fontPath}/${fontName}"];
}
else {
$record = array();
}

// 最后一次提交大于当前记录时间
if (-1 == $timestamp || $record['timestamp'] > $timestamp) {
$fontFile = "${fontPath}/${fontName}.${fontType}";
if (file_exists($fontFile)) {
$data = array(
'fontName' => $fontName,
'hasNew' => 1,
'timestamp' => empty($record['timestamp']) ? getTimestamp() : $record['timestamp'],
'fontType' => $fontType,
);
$data[$fontType] = readBase64File($fontFile);
jsonp(0, $data);
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion demo/sync/list.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cicons":{"user":"3e60a8b0712c80d976fb3e917c658d4f","timestamp":1458308845195,"fontType":"ttf"}}
{"D:\\fonteditor\\demo\\sync\/fonteditor":{"user":"3e60a8b0712c80d976fb3e917c658d4f","timestamp":1458479047942,"fontType":"ttf"}}
Loading

0 comments on commit f11da19

Please sign in to comment.