Skip to content

Commit 8e8d00e

Browse files
author
Manus AI
committed
Add entity photo upload support and refactor file handling to use FileResolverService
- Add photo upload support for entities in Payload::setEntities() and Payload::insertEntities() - Supports all file input types: UploadedFile, Base64, resource ID, and URL - Refactor ContactController create/update methods to use FileResolverService - Refactor DriverController create/update methods to use FileResolverService - Eliminates code duplication and provides consistent file handling - Leverages new FileResolverService from fleetbase/core-api v1.6.35
1 parent 2c52509 commit 8e8d00e

3 files changed

Lines changed: 50 additions & 64 deletions

File tree

server/src/Http/Controllers/Api/v1/ContactController.php

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,13 @@ public function create(CreateContactRequest $request)
2929
$input['phone'] = is_string($input['phone']) ? Utils::formatPhoneNumber($input['phone']) : $input['phone'];
3030
$input['type'] = empty($input['type']) ? 'contact' : $input['type'];
3131

32-
// Handle photo as either file id/ or base64 data string
33-
$photo = $request->input('photo');
34-
if ($photo) {
35-
// Handle photo being a file id
36-
if (Utils::isPublicId($photo)) {
37-
$file = File::where('public_id', $photo)->first();
38-
if ($file) {
39-
$input['photo_uuid'] = $file->uuid;
40-
}
41-
}
32+
// Handle photo upload using FileResolverService
33+
if ($request->has('photo')) {
34+
$path = 'uploads/' . session('company') . '/contacts';
35+
$file = app(\Fleetbase\Services\FileResolverService::class)->resolve($request->input('photo'), $path);
4236

43-
// Handle the photo being base64 data string
44-
if (Utils::isBase64String($photo)) {
45-
$path = implode('/', ['uploads', session('company'), 'contacts']);
46-
$file = File::createFromBase64($photo, null, $path);
47-
if ($file) {
48-
$input['photo_uuid'] = $file->uuid;
49-
}
37+
if ($file) {
38+
$input['photo_uuid'] = $file->uuid;
5039
}
5140
}
5241

@@ -101,29 +90,20 @@ public function update($id, UpdateContactRequest $request)
10190
]);
10291
}
10392

104-
// Handle photo as either file id/ or base64 data string
105-
$photo = $request->input('photo');
106-
if ($photo) {
107-
// Handle photo being a file id
108-
if (Utils::isPublicId($photo)) {
109-
$file = File::where('public_id', $photo)->first();
110-
if ($file) {
111-
$input['photo_uuid'] = $file->uuid;
112-
}
113-
}
114-
115-
// Handle the photo being base64 data string
116-
if (Utils::isBase64String($photo)) {
117-
$path = implode('/', ['uploads', session('company'), 'customers']);
118-
$file = File::createFromBase64($photo, null, $path);
119-
if ($file) {
120-
$input['photo_uuid'] = $file->uuid;
121-
}
122-
}
93+
// Handle photo upload using FileResolverService
94+
if ($request->has('photo')) {
95+
$photo = $request->input('photo');
12396

12497
// Handle removal key
12598
if ($photo === 'REMOVE') {
12699
$input['photo_uuid'] = null;
100+
} else {
101+
$path = 'uploads/' . session('company') . '/contacts';
102+
$file = app(\Fleetbase\Services\FileResolverService::class)->resolve($photo, $path);
103+
104+
if ($file) {
105+
$input['photo_uuid'] = $file->uuid;
106+
}
127107
}
128108
}
129109

server/src/Http/Controllers/Api/v1/DriverController.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,10 @@ public function create(CreateDriverRequest $request)
124124
// create the driver
125125
$driver = Driver::create($input);
126126

127-
// Handle photo as either file id/ or base64 data string
128-
$photo = $request->input('photo');
129-
if ($photo) {
130-
$file = null;
131-
// Handle photo being a file id
132-
if (Utils::isPublicId($photo)) {
133-
$file = File::where('public_id', $photo)->first();
134-
}
135-
136-
// Handle the photo being base64 data string
137-
if (Utils::isBase64String($photo)) {
138-
$path = implode('/', ['uploads', session('company'), 'drivers']);
139-
$file = File::createFromBase64($photo, null, $path);
140-
}
127+
// Handle photo upload using FileResolverService
128+
if ($request->has('photo')) {
129+
$path = 'uploads/' . $company->uuid . '/drivers';
130+
$file = app(\Fleetbase\Services\FileResolverService::class)->resolve($request->input('photo'), $path);
141131

142132
if ($file) {
143133
$user->update(['photo_uuid' => $file->uuid]);
@@ -218,20 +208,10 @@ public function update($id, UpdateDriverRequest $request)
218208
$driver->update($input);
219209
$driver->flushAttributesCache();
220210

221-
// Handle photo as either file id/ or base64 data string
222-
$photo = $request->input('photo');
223-
if ($photo) {
224-
$file = null;
225-
// Handle photo being a file id
226-
if (Utils::isPublicId($photo)) {
227-
$file = File::where('public_id', $photo)->first();
228-
}
229-
230-
// Handle the photo being base64 data string
231-
if (Utils::isBase64String($photo)) {
232-
$path = implode('/', ['uploads', session('company'), 'drivers']);
233-
$file = File::createFromBase64($photo, null, $path);
234-
}
211+
// Handle photo upload using FileResolverService
212+
if ($request->has('photo')) {
213+
$path = 'uploads/' . session('company') . '/drivers';
214+
$file = app(\Fleetbase\Services\FileResolverService::class)->resolve($request->input('photo'), $path);
235215

236216
if ($file) {
237217
$driver->user->update(['photo_uuid' => $file->uuid]);

server/src/Models/Payload.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ public function setEntities($entities = [])
245245
}
246246
}
247247

248+
// Handle entity photo upload
249+
if (isset($attributes['photo'])) {
250+
$path = 'uploads/' . session('company') . '/entities';
251+
$file = app(\Fleetbase\Services\FileResolverService::class)->resolve($attributes['photo'], $path);
252+
253+
if ($file) {
254+
$attributes['photo_uuid'] = $file->uuid;
255+
}
256+
257+
// Clean up raw photo data
258+
unset($attributes['photo']);
259+
}
260+
248261
$entity = new Entity($attributes);
249262
$this->entities()->save($entity);
250263
}
@@ -286,6 +299,19 @@ public function insertEntities($entities = [])
286299
}
287300
}
288301

302+
// Handle entity photo upload
303+
if (isset($attributes['photo'])) {
304+
$path = 'uploads/' . session('company') . '/entities';
305+
$file = app(\Fleetbase\Services\FileResolverService::class)->resolve($attributes['photo'], $path);
306+
307+
if ($file) {
308+
$attributes['photo_uuid'] = $file->uuid;
309+
}
310+
311+
// Clean up raw photo data
312+
unset($attributes['photo']);
313+
}
314+
289315
Entity::insertGetUuid($attributes, $this);
290316
}
291317

0 commit comments

Comments
 (0)