Source code of filesite.io. https://filesite.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 lines
5.4 KiB

<?php
/**
* List Controller
*/
require_once __DIR__ . '/../../../lib/DirScanner.php';
require_once __DIR__ . '/../../../plugins/Parsedown.php';
require_once __DIR__ . '/SiteController.php';
Class ApiController extends SiteController {
public function actionIndex() {
$code = 0;
$err = 'Not allowed';
return $this->renderJson(compact('code', 'err'));
}
public function actionTags() {
//获取数据
$menus = array(); //菜单,一级目录
$htmlReadme = ''; //Readme.md 内容,底部网站详细介绍
$htmlCateReadme = ''; //当前目录下的Readme.md 内容
$menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序
$scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3);
$code = 1;
$msg = '';
$err = '';
//获取tags分类
$noFiles = true;
$data = $this->getTags($dirTree, $noFiles);
return $this->renderJson(compact('code', 'msg', 'err', 'data'));
}
/*
* 参数:
* content: 从抖音或其它平台复制出来的视频分享内容,或者视频网址
* title: 视频标题
* tag: 分类名称
* tagid: 分类id
* 其中title、tag和tagid为可选值。
*/
public function actionAddfav() {
$content = $this->post('content', '');
$title = $this->post('title', '');
$tag = $this->post('tag', '');
$tagid = $this->post('tagid', '');
$code = 1;
$msg = '';
$err = '';
if (empty($content)) {
$code = 0;
$err = '请粘贴填写分享内容!';
}
$tagName = '';
if (!empty($tag) || !empty($tagid)) { //检查分类名称或id是否存在
$scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3);
//获取tags分类
$tags = $this->getTags($dirTree);
if (!empty($tagid) && empty($tags[$tagid])) { //检查tagid是否存在
$code = 0;
$err = "分类ID {$tagid} 不存在!";
}
if (!empty($tag)) { //检查tag是否存在
$tag_exists = false;
foreach($tags as $id => $item) {
if ($item['name'] == $tag) {
$tag_exists = true;
$tagName = $tag;
break;
}
}
if ($tag_exists == false) {
$code = 0;
$err = "分类 {$tag} 不存在!";
}
}
if (empty($tagName) && !empty($tags[$tagid])) {
$tagName = $tags[$tagid]['name'];
}
}
if ($code == 1) { //保存视频
$msg = $this->saveShareVideo($content, $title, $tagName) ? '视频保存完成。' : '视频保存失败!';
}
return $this->renderJson(compact('code', 'msg', 'err'));
}
protected function getVideoId($url) {
return md5($url);
}
//保存分享视频
protected function saveShareVideo($content, $title, $tagName) {
$done = true;
$newVideo = array();
preg_match("/https:\/\/[\w\.]+\/\w+/i", $content, $matches);
if (!empty($matches)) {
$newVideo['url'] = $matches[0];
$done = $done && $this->saveBotTask($newVideo['url']);
}
if (!empty($tagName)) {
$done = $done && $this->saveVideoToTag($newVideo['url'], $tagName);
}
return $done;
}
//保存分享视频到任务文件
protected function saveBotTask($url) {
$task_dir = __DIR__ . '/../../../runtime/' . FSC::$app['config']['tajian']['task_dir'];
if (!is_dir($task_dir)) {
mkdir($task_dir, 0755, true);
}
$video_id = $this->getVideoId($url);
$filepath = "{$task_dir}/{$video_id}.task";
return file_put_contents($filepath, $url) !== false;
}
//保存分享视频到tag分类
//TODO: 如果高并发,需要避免数据被覆盖的问题
protected function saveVideoToTag($url, $tagName) {
$tag_dir = __DIR__ . '/../../../www/'
. FSC::$app['config']['content_directory']
. '/' . FSC::$app['config']['tajian']['tag_dir'];
if (!is_dir($tag_dir)) {
mkdir($tag_dir, 0755, true);
}
$video_id = $this->getVideoId($url);
$filepath = "{$tag_dir}/{$tagName}.txt";
if (file_exists($filepath)) {
$content = file_get_contents($filepath);
$videos = explode("\n", $content);
$last_id = array_pop($videos);
if (!empty($last_id)) {
array_push($videos, $last_id);
}
if (!in_array($video_id, $videos)) {
array_push($videos, $video_id);
}
return file_put_contents($filepath, implode("\n", $videos)) !== false;
}else {
return file_put_contents($filepath, $vidoe_id) !== false;
}
}
}