1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
class SiteController extends Controller
{
public static $MODELS = [
'User',
'File',
'Link',
'Tag',
'Writing',
'Role',
];
public static $MODELS_LC_PLURAL = [
'users',
'files',
'links',
'tags',
'writings',
'roles',
];
//returns an env mapping to be used by client. pretty much a subset of laravel app's env, but might also have addition things
public static function env(){
$vars = [
'API_URL' => $_ENV['API_URL'], //TODO use config() here instead of env. env should only be used in the config files
'WS_URL' => $_ENV['WS_URL'],
'FILEUPLOAD_URL' => $_ENV['FILEUPLOAD_URL'] ?? '', //these 3 file vars are from old 'thesite'. fileup_url might end up being same as api_url
'FILEUPLOAD_MAX_MB' => $_ENV['FILEUPLOAD_MAX_MB'] ?? 10,
'FILEUPLOAD_CHUNK_MB' => $_ENV['FILEUPLOAD_CHUNK_MB'] ?? '',
'MODE' => config('app.env'), //dev, prod,
];
return $vars;
}
//get a random dune quote
public static function duneQuote(){
$jsonFile = Storage::disk('local')->get('duneprechapterquotes.json');
$quotes = json_decode($jsonFile, true);
$book = array_rand($quotes); //random book
$quoteJSON = $quotes[$book][array_rand($quotes[$book])];
$html = "<h5>${book}</h5><p><i>${quoteJSON['quote']}</i><br> - <small>${quoteJSON['author']}</small></p>";
return $html;
}
public function uploadFiles(Request $req){
$files = $req->file('f');
$dir = $_ENV['FILEUPLOAD_DIR'] ?? 'uploads';
$returnJSON = true;
if ($req->input('response_format') == 'html'){
$returnJSON = false;
}
$res = [
'num_files' => sizeof($files),
'num_failed'=> 0,
'num_uploaded' => 0,
'success' => false,
'files' => []
];
//validate file
$maxsize = $_ENV['FILEUPLOAD_MAX_MB'] * 1024 ?? 10240; //SiteController::env()['FILEUPLOAD_MAX_MB'] * 1024;
$validated = $req->validate([
'f' => "required|array|max:${maxsize}",
'f.*' => "required|file|max:${maxsize}"
]);
foreach ($files as $f){
$filename = $f->getClientOriginalName();
if (Storage::disk('public')->exists("${dir}/${filename}")){
$filename = Carbon::now()->timestamp.'_'.$filename;
}
$path = $f->storeAs(
$dir,
$filename,
'public'
);
if ($path){
$res['num_uploaded'] += 1;
array_push($res['files'], $path);
} else {
$res['num_failed'] += 1;
}
}
if ($res['num_uploaded'] == sizeof($files)){
$res['success'] = true;
}
if ($returnJSON){
return $res;
} else {
//return "File uploaded: ${filename} ";
return redirect("f/${filename}"); //TODO homepage with data flashing (->with())
}
}
public function search4chan(Request $req){
$query = $req->input('query');
Log::info('search4chan()');
$cmd = 'python ./4chansearch.py ' . escapeshellarg($query);
if ($req->input('board') != null) {
$cmd .= ' -b ' . escapeshellarg($req->input('board'));
}
exec($cmd, $res, $ret);
return $res;
}
//used for testing various things, from /test routes
public function test(Request $req){
//return $req->header('user_agent');
return config('app.env');
}
public function updateSession(){
}
//todo 2/14
public function importItems(Request $request) {
if (Auth::user()->role != 0) { //admin
return response()->json([
'success' => false,
'message' => 'Unauthorized'
], 401);
}
if (isset($request->modelType)){ //model type must be specified as a string
$modelClass = 'App\\Models\\' . ucfirst($request->modelType);
if (class_exists($modelClass)){
$jsonFile = $request->file('jsonFile');
if ($jsonFile == null){
return response()->json([
'success' => false,
'message' => 'No file uploaded'
], 400);
}
$f = str($jsonFile->get());
$items = json_decode($f); //this should be an array
//TODO verify valid json
if (!is_array($items)) {
return response()->json([
'success' => false,
'message' => 'json in submitted file must be an array of items'
], 400);
}
$imported = 0;
$failed = 0;
$errors = [];
foreach ($items as $item) {
try {
$instance = new $modelClass();
foreach ($instance->fillable as $field) {
if (isset($item->$field)){
$instance->$field = $item->$field;
}
}
$instance->user_id = Auth::user()->id;
$instance->saveOrFail();
$imported++;
} catch (\Exception $e) {
$failed++;
$errors[] = "Failed to import item: " . $e->getMessage();
}
}
return response()->json([
'success' => $failed === 0,
'imported' => $imported,
'failed' => $failed,
'errors' => $errors
]);
} else {
return response()->json([
"success"=> false,
'error' => 'model type does not exist'
]);
}
} else {
return response()->json([
'success'=> false,
'error'=> 'no modelType specified'
]);
}
}
}
|