Skip to content

Commit 18de04d

Browse files
author
Dan Jewett
committed
* 'master' of https://github.com/rootinc/laravel-s3-file-model: Add update method test for s3 filesystem Tests updates and method docblocks
2 parents 04a1532 + 9926aa9 commit 18de04d

File tree

3 files changed

+592
-509
lines changed

3 files changed

+592
-509
lines changed

src/FileBaseController.php

Lines changed: 159 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -9,162 +9,162 @@
99

1010
class FileBaseController extends Controller
1111
{
12-
public function index(Request $request)
13-
{
14-
$search = $request->input('search') ? $request->input('search') : "";
15-
16-
$files = File::where(function($q) use ($search) {
17-
$q->where('file_name', 'ILIKE', "%$search%")
18-
->orWhere('title', 'ILIKE', "%$search%");
19-
})
20-
->orderBy('file_name')
21-
->paginate();
22-
23-
return response()->json([
24-
'status' => "success",
25-
'payload' => [
26-
'files' => $files,
27-
]
28-
]);
29-
}
30-
31-
/**
32-
* Creates and uploads a file
33-
*
34-
* @param Illuminate\Http\Request $request
35-
* @return Illuminate\Http\Response
36-
*/
37-
public function store(Request $request)
38-
{
39-
$file_name = $request->input('file_name');
40-
$file_type = $request->input('file_type');
41-
$file_data = $request->input('file_data');
42-
43-
$relative_directory = $request->input('directory') ?? null;
44-
$public = $request->input('public') ?? false;
45-
46-
//this is set up with two distinct routes so that the FileUploader component can still call the `store / post` method on the file object
47-
//in the cloud route, we are doing a direct upload to s3
48-
//in the other route, we are doing a an upload to the server, then to s3
49-
if (!$file_data)
50-
{
51-
$data = File::s3CreateUpload(new File, $file_name, $file_type, $relative_directory, $public);
52-
$file = $data['file'];
53-
$upload_url = $data['upload_url'];
54-
55-
$file->refresh();
56-
57-
return response()->json([
58-
'status' => "success",
59-
'payload' => [
60-
'file' => $file,
61-
'upload_url' => $upload_url,
62-
]
63-
]);
64-
}
65-
else
66-
{
67-
$file = File::uploadAndCreateFileFromDataURI($file_name, $file_type, $file_data, $relative_directory, $public);
68-
$file->refresh();
69-
70-
return response()->json([
71-
'status' => "success",
72-
'payload' => [
73-
'file' => $file
74-
]
75-
]);
76-
}
77-
}
78-
79-
/**
80-
* Updates and uploads replacement file
81-
*
82-
* @param Illuminate\Http\Request $request
83-
* @return Illuminate\Http\Response
84-
*/
85-
public function update(Request $request, File $file)
86-
{
87-
//this is set up with two distinct routes so that the FileUploader component can still call the `update / put` method on the file object
88-
//in the title route, we only need to update the title of the file
89-
//in the other route, we are doing a replacement file
90-
if ($request->input('title'))
91-
{
92-
$file->title = $request->input('title');
93-
$file->save();
94-
95-
return response()->json([
96-
'status' => "success",
97-
'payload' => [
98-
'file' => $file
99-
]
100-
]);
101-
}
102-
else
103-
{
104-
File::deleteUpload($file->location);
105-
106-
$file_name = $request->input('file_name');
107-
$file_type = $request->input('file_type');
108-
$file_data = $request->input('file_data');
109-
110-
$relative_directory = $request->input('directory') ?? null;
111-
$public = $request->input('public') ?? false;
112-
113-
//this is set up with two distinct routes so that the FileUploader component can still call the `update / put` method on the file object
114-
//in the cloud route, we are doing a direct upload to s3
115-
//in the other route, we are doing a an upload to the server, then to s3
116-
if (!$file_data)
117-
{
118-
$data = File::s3CreateUpload($file, $file_name, $file_type, $relative_directory, $public);
119-
$file = $data['file'];
120-
$upload_url = $data['upload_url'];
121-
122-
$file->refresh();
123-
124-
return response()->json([
125-
'status' => "success",
126-
'payload' => [
127-
'file' => $file,
128-
'upload_url' => $upload_url,
129-
]
130-
]);
131-
}
132-
else
133-
{
134-
$uploadedFile = File::makeUploadFileFromDataURI($file_name, $file_type, $file_data, $relative_directory, $public);
135-
$upload_location = File::upload($uploadedFile, null, true);
136-
137-
$file->file_name = $file_name;
138-
$file->file_type = $uploadedFile->getClientMimeType();
139-
$file->location = $upload_location;
140-
141-
$file->save();
142-
143-
return response()->json([
144-
'status' => "success",
145-
'payload' => [
146-
'file' => $file
147-
]
148-
]);
149-
}
150-
}
151-
}
152-
153-
/**
154-
* Deletes and deletes uploaded file
155-
*
156-
* @param Illuminate\Http\Request $request
157-
* @return Illuminate\Http\Response
158-
*/
159-
public function destroy(Request $request, File $file)
160-
{
161-
File::deleteUpload($file->location);
162-
163-
$file->delete();
164-
165-
return response()->json([
166-
'status' => "success",
167-
'payload' => []
168-
]);
169-
}
170-
}
12+
public function index(Request $request)
13+
{
14+
$search = $request->input('search') ? $request->input('search') : "";
15+
16+
$files = File::where(function($q) use ($search) {
17+
$q->where('file_name', 'ILIKE', "%$search%")
18+
->orWhere('title', 'ILIKE', "%$search%");
19+
})
20+
->orderBy('file_name')
21+
->paginate();
22+
23+
return response()->json([
24+
'status' => "success",
25+
'payload' => [
26+
'files' => $files,
27+
]
28+
]);
29+
}
30+
31+
/**
32+
* Creates and uploads a file
33+
*
34+
* @param \Illuminate\Http\Request $request
35+
* @return \Illuminate\Http\JsonResponse
36+
*/
37+
public function store(Request $request)
38+
{
39+
$file_name = $request->input('file_name');
40+
$file_type = $request->input('file_type');
41+
$file_data = $request->input('file_data');
42+
43+
$relative_directory = $request->input('directory') ?? null;
44+
$public = $request->input('public') ?? false;
45+
46+
//this is set up with two distinct routes so that the FileUploader component can still call the `store / post` method on the file object
47+
//in the cloud route, we are doing a direct upload to s3
48+
//in the other route, we are doing a an upload to the server, then to s3
49+
if (!$file_data)
50+
{
51+
$data = File::s3CreateUpload(new File, $file_name, $file_type, $relative_directory, $public);
52+
$file = $data['file'];
53+
$upload_url = $data['upload_url'];
54+
55+
$file->refresh();
56+
57+
return response()->json([
58+
'status' => "success",
59+
'payload' => [
60+
'file' => $file,
61+
'upload_url' => $upload_url,
62+
]
63+
]);
64+
}
65+
else
66+
{
67+
$file = File::uploadAndCreateFileFromDataURI($file_name, $file_type, $file_data, $relative_directory, $public);
68+
$file->refresh();
69+
70+
return response()->json([
71+
'status' => "success",
72+
'payload' => [
73+
'file' => $file
74+
]
75+
]);
76+
}
77+
}
78+
79+
/**
80+
* Updates and uploads replacement file
81+
*
82+
* @param \Illuminate\Http\Request $request
83+
* @return \Illuminate\Http\JsonResponse
84+
*/
85+
public function update(Request $request, File $file)
86+
{
87+
//this is set up with two distinct routes so that the FileUploader component can still call the `update / put` method on the file object
88+
//in the title route, we only need to update the title of the file
89+
//in the other route, we are doing a replacement file
90+
if ($request->input('title'))
91+
{
92+
$file->title = $request->input('title');
93+
$file->save();
94+
95+
return response()->json([
96+
'status' => "success",
97+
'payload' => [
98+
'file' => $file
99+
]
100+
]);
101+
}
102+
else
103+
{
104+
File::deleteUpload($file->location);
105+
106+
$file_name = $request->input('file_name');
107+
$file_type = $request->input('file_type');
108+
$file_data = $request->input('file_data');
109+
110+
$relative_directory = $request->input('directory') ?? null;
111+
$public = $request->input('public') ?? false;
112+
113+
//this is set up with two distinct routes so that the FileUploader component can still call the `update / put` method on the file object
114+
//in the cloud route, we are doing a direct upload to s3
115+
//in the other route, we are doing a an upload to the server, then to s3
116+
if (!$file_data)
117+
{
118+
$data = File::s3CreateUpload($file, $file_name, $file_type, $relative_directory, $public);
119+
$file = $data['file'];
120+
$upload_url = $data['upload_url'];
121+
122+
$file->refresh();
123+
124+
return response()->json([
125+
'status' => "success",
126+
'payload' => [
127+
'file' => $file,
128+
'upload_url' => $upload_url,
129+
]
130+
]);
131+
}
132+
else
133+
{
134+
$uploadedFile = File::makeUploadFileFromDataURI($file_name, $file_type, $file_data);
135+
$upload_location = File::upload($uploadedFile, $relative_directory, $public);
136+
137+
$file->file_name = $file_name;
138+
$file->file_type = $uploadedFile->getClientMimeType();
139+
$file->location = $upload_location;
140+
141+
$file->save();
142+
143+
return response()->json([
144+
'status' => "success",
145+
'payload' => [
146+
'file' => $file
147+
]
148+
]);
149+
}
150+
}
151+
}
152+
153+
/**
154+
* Deletes and deletes uploaded file
155+
*
156+
* @param \Illuminate\Http\Request $request
157+
* @return \Illuminate\Http\JsonResponse
158+
*/
159+
public function destroy(Request $request, File $file)
160+
{
161+
File::deleteUpload($file->location);
162+
163+
$file->delete();
164+
165+
return response()->json([
166+
'status' => "success",
167+
'payload' => []
168+
]);
169+
}
170+
}

0 commit comments

Comments
 (0)