mobile app api
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
require __DIR__ . '/_bootstrap.php';
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/home-photo.php",
|
||||
* tags={"Homes"},
|
||||
* summary="Upload property main photo",
|
||||
* security={{"bearerAuth":{}}},
|
||||
* @OA\RequestBody(required=true, @OA\MediaType(mediaType="multipart/form-data",
|
||||
* @OA\Schema(
|
||||
* required={"idhome","photo"},
|
||||
* @OA\Property(property="idhome", type="integer"),
|
||||
* @OA\Property(property="photo", type="string", format="binary", description="image")
|
||||
* )
|
||||
* )),
|
||||
* @OA\Response(response=200, description="OK", @OA\JsonContent(
|
||||
* @OA\Property(property="data", ref="#/components/schemas/Home"))),
|
||||
* @OA\Response(response=403, ref="#/components/responses/Forbidden"),
|
||||
* @OA\Response(response=422, ref="#/components/responses/ValidationError")
|
||||
* )
|
||||
*/
|
||||
require_method('POST');
|
||||
$user = require_auth($pdo);
|
||||
$config = require __DIR__ . '/config.php';
|
||||
|
||||
$idhome = (int) ($_POST['idhome'] ?? 0);
|
||||
$fields = [];
|
||||
if ($idhome <= 0) {
|
||||
$fields['idhome'] = ['Required'];
|
||||
}
|
||||
if (empty($_FILES['photo']) || $_FILES['photo']['error'] !== UPLOAD_ERR_OK) {
|
||||
$fields['photo'] = ['Valid image is required'];
|
||||
} elseif (!is_allowed_upload($_FILES['photo']['tmp_name'], $_FILES['photo']['name'], true)) {
|
||||
$fields['photo'] = ['Only image files are allowed'];
|
||||
}
|
||||
if ($fields) {
|
||||
json_error(422, 'Validation failed', $fields);
|
||||
}
|
||||
if (!user_owns_home($pdo, $user, $idhome)) {
|
||||
json_error(403, 'No access to this home');
|
||||
}
|
||||
|
||||
$dir = $config['homedocs_dir'];
|
||||
if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) {
|
||||
json_error(500, 'Storage directory unavailable');
|
||||
}
|
||||
|
||||
$safe = preg_replace('/[^A-Za-z0-9._-]/', '_', basename($_FILES['photo']['name']));
|
||||
$filename = $idhome . '-' . $user['id'] . '-' . time() . '-' . $safe;
|
||||
|
||||
if (!move_uploaded_file($_FILES['photo']['tmp_name'], $dir . '/' . $filename)) {
|
||||
json_error(500, 'Failed to store photo');
|
||||
}
|
||||
|
||||
$pdo->prepare('UPDATE home SET mainphoto = ? WHERE idhome = ? AND iduser = ?')
|
||||
->execute([$filename, $idhome, $user['id']]);
|
||||
|
||||
$stmt = $pdo->prepare('SELECT * FROM home WHERE idhome = ? LIMIT 1');
|
||||
$stmt->execute([$idhome]);
|
||||
|
||||
json_data(present_home($stmt->fetch(), true, home_counts($pdo, [$idhome])[$idhome] ?? null));
|
||||
Reference in New Issue
Block a user