Skip to content

Commit

Permalink
新增文件预览功能
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Dec 10, 2021
1 parent a303a60 commit aa9a323
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 126 deletions.
35 changes: 18 additions & 17 deletions app/Http/Controllers/Api/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,44 +407,45 @@ public function content__upload()
$path = 'uploads/office/' . date("Ym") . '/u' . $user->userid . '/';
$data = Base::upload([
"file" => Request::file('files'),
"type" => 'office',
"type" => 'more',
"autoThumb" => false,
"path" => $path,
]);
if (Base::isError($data)) {
return $data;
}
$data = $data['data'];
//
$type = "";
switch ($data['ext']) {
case 'doc':
case 'docx':
$type = "word";
break;
case 'xls':
case 'xlsx':
$type = "excel";
break;
case 'ppt':
case 'pptx':
$type = "ppt";
break;
}
$type = match ($data['ext']) {
'doc', 'docx' => "word",
'xls', 'xlsx' => "excel",
'ppt', 'pptx' => "ppt",
'txt', 'html', 'htm', 'asp', 'jsp', 'xml', 'json', 'properties', 'md', 'gitignore', 'log', 'java', 'py', 'c', 'cpp', 'sql', 'sh', 'bat', 'm', 'bas', 'prg', 'cmd' => "text",
'jpg', 'jpeg', 'png', 'gif' => 'image',
'zip', 'rar', 'jar', 'tar', 'gzip' => 'compress',
'mp3', 'wav', 'mp4', 'flv' => 'media',
'pdf' => 'pdf',
'dwg' => 'cad',
default => "",
};
$file = File::createInstance([
'pid' => $pid,
'name' => Base::rightDelete($data['name'], '.' . $data['ext']),
'type' => $type,
'ext' => $data['ext'],
'userid' => $userid,
'created_id' => $user->userid,
]);
// 开始创建
return AbstractModel::transaction(function () use ($user, $data, $file) {
return AbstractModel::transaction(function () use ($type, $user, $data, $file) {
$file->save();
//
$content = FileContent::createInstance([
'fid' => $file->id,
'content' => [
'from' => '',
'type' => $type,
'ext' => $data['ext'],
'url' => $data['path']
],
'text' => '',
Expand Down
9 changes: 5 additions & 4 deletions app/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
use Request;

/**
* Class File
* App\Models\File
*
* @package App\Models
* @property int $id
* @property int|null $pid 上级ID
* @property int|null $cid 复制ID
* @property string|null $name 名称
* @property string|null $type 类型
* @property string|null $ext 后缀名
* @property int|null $size 大小(B)
* @property int|null $userid 拥有者ID
* @property int|null $share 是否共享(1:共享所有人,2:指定成员)
* @property int|null $created_id 创建者ID
* @property int|null $share 是否共享
* @property int|null $created_id 创建者
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
Expand All @@ -33,6 +33,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|File whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|File whereCreatedId($value)
* @method static \Illuminate\Database\Eloquent\Builder|File whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|File whereExt($value)
* @method static \Illuminate\Database\Eloquent\Builder|File whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|File whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|File wherePid($value)
Expand Down
42 changes: 22 additions & 20 deletions app/Models/FileContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,28 @@ public static function formatContent($type, $content)
return Response::download(public_path($content['url']));
}
if (empty($content)) {
switch ($type) {
case 'document':
$content = [
"type" => "md",
"content" => "",
];
break;

case 'sheet':
$content = [
[
"name" => "Sheet1",
"config" => json_decode('{}'),
]
];
break;

default:
$content = json_decode('{}');
break;
$content = match ($type) {
'document' => [
"type" => "md",
"content" => "",
],
'sheet' => [
[
"name" => "Sheet1",
"config" => json_decode('{}'),
]
],
default => json_decode('{}'),
};
} else {
$content['preview'] = false;
if ($content['ext'] && !in_array($content['ext'], ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'])) {
$url = 'http://' . env('APP_IPPR') . '.3/' . $content['url'];
if ($type == 'image') {
$url = Base::fillUrl($content['url']);
}
$content['url'] = base64_encode($url);
$content['preview'] = true;
}
}
return Base::retSuccess('success', [ 'content' => $content ]);
Expand Down
15 changes: 10 additions & 5 deletions app/Module/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2218,17 +2218,22 @@ public static function upload($param)
case 'file':
$type = ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz'];
break;
case 'office':
$type = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'];
break;
case 'firmware':
$type = ['img', 'tar', 'bin'];
break;
case 'md':
$type = ['md'];
break;
case 'node_template':
$type = ['csv'];
case 'more':
$type = [
'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx',
'txt', 'html', 'htm', 'asp', 'jsp', 'xml', 'json', 'properties', 'md', 'gitignore', 'log', 'java', 'py', 'c', 'cpp', 'sql', 'sh', 'bat', 'm', 'bas', 'prg', 'cmd',
'jpg', 'jpeg', 'png', 'gif',
'zip', 'rar', 'jar', 'tar', 'gzip',
'mp3', 'wav', 'mp4', 'flv',
'pdf',
'dwg'
];
break;
default:
return Base::retError('错误的类型参数');
Expand Down
47 changes: 47 additions & 0 deletions database/migrations/2021_12_10_170751_files_add_ext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class FilesAddExt extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$isAdd = false;
Schema::table('files', function (Blueprint $table) use (&$isAdd) {
if (!Schema::hasColumn('files', 'ext')) {
$isAdd = true;
$table->string('ext', 20)->nullable()->default('')->after('type')->comment('后缀名');
}
});
if ($isAdd) {
// 更新数据
\App\Models\File::chunkById(100, function ($lists) {
foreach ($lists as $item) {
if (in_array($item->type, ['word', 'excel', 'ppt'])) {
$item->ext = str_replace(['word', 'excel', 'ppt'], ['docx', 'xlsx', 'pptx'], $item->type);
$item->save();
}
}
});
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('files', function (Blueprint $table) {
$table->dropColumn("ext");
});
}
}
14 changes: 12 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ services:
networks:
extnetwork:
ipv4_address: "${APP_IPPR}.3"
depends_on:
- php
links:
- php
- office
- fileview
restart: unless-stopped

redis:
Expand Down Expand Up @@ -95,6 +94,17 @@ services:
ipv4_address: "${APP_IPPR}.6"
restart: unless-stopped

fileview:
container_name: "dootask-fileview-${APP_ID}"
image: "kuaifan/fileview:4.1.0"
environment:
TZ: "Asia/Shanghai"
KK_CONTEXT_PATH: "/fileview"
networks:
extnetwork:
ipv4_address: "${APP_IPPR}.7"
restart: unless-stopped

networks:
extnetwork:
name: "dootask-networks-${APP_ID}"
Expand Down
39 changes: 33 additions & 6 deletions docker/nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ upstream office {
server office weight=5 max_fails=3 fail_timeout=30s;
keepalive 16;
}
upstream fileview {
server fileview:8012 weight=5 max_fails=3 fail_timeout=30s;
keepalive 16;
}
server {
listen 80;

Expand All @@ -32,10 +36,12 @@ server {
allow all;
}

location ~* ^/(6.3.1-32|cache/files|web-apps/apps)/ {
location =/ws {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
Expand All @@ -45,13 +51,32 @@ server {
proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://office;
proxy_pass http://service;
}

location =/ws {
location @laravels {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header Server-Protocol $server_protocol;
proxy_set_header Server-Name $server_name;
proxy_set_header Server-Addr $server_addr;
proxy_set_header Server-Port $server_port;
proxy_pass http://service;
}

location /office/ {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Host $http_host/office;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
Expand All @@ -61,22 +86,24 @@ server {
proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://service;
proxy_pass http://office/;
}

location @laravels {
location /fileview {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header Server-Protocol $server_protocol;
proxy_set_header Server-Name $server_name;
proxy_set_header Server-Addr $server_addr;
proxy_set_header Server-Port $server_port;
proxy_pass http://service;
proxy_pass http://fileview;
}
}

Expand Down
2 changes: 1 addition & 1 deletion resources/assets/js/components/OnlyOffice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default {
if (!url) {
return;
}
$A.loadScript(this.$store.state.method.apiUrl("../web-apps/apps/api/documents/api.js"), () => {
$A.loadScript(this.$store.state.method.apiUrl("../office/web-apps/apps/api/documents/api.js"), () => {
this.loadFile()
})
},
Expand Down
Loading

0 comments on commit aa9a323

Please sign in to comment.