Skip to content

Commit

Permalink
idk what im pushing
Browse files Browse the repository at this point in the history
  • Loading branch information
killua-eu committed Jan 15, 2024
1 parent 0ae13ed commit 238b275
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 44 deletions.
40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,16 @@ TODO: token

```bash
#new
curl -k -X POST https://glued/api/stor/v1/buckets/1fcb4d5c-5364-4cf3-b24e-070c4c71f8d2/objects -H 'Authorization: Bearer gtk_35MCHFgkNh1PymQEOLStzMtESdo4DZXykoYWvjX9QcQ=' -H 'Content-Type: multipart/form-data' -F 'refs={"Ryanair.pdf":{"refs":{"predecessor":"77384ad2-80a4-11ee-9edc-9747c96a1231","sekatko:vpd":"bd4a3c4a-80a6-11ee-8e34-67217a8a9f66"},"meta":{"sekato":{"ocr":"sometext"},"otherapp":{"whatever":"https://glued.example.com"}}}};type=application/json' -F 'file[]=@/home/killua/Ryanair.pdf' -F 'file[]=@/home/killua/eastern loves_INDUSTRA.png' -F 'file[]=@/home/killua/todo-petru' -F 'field1=fiels2' | jq .


{
"Ryanair.pdf": {
"refs": {
"predecessor": "77384ad2-80a4-11ee-9edc-9747c96a1231",
"sekatko:vpd": "bd4a3c4a-80a6-11ee-8e34-67217a8a9f66"
},
"meta": {
"sekato": {
"ocr": "sometext"
},
"otherapp": {
"whatever": "https://glued.example.com"
}
}
}
}

curl -k -X POST https://glued/api/stor/v1/buckets/1fcb4d5c-5364-4cf3-b24e-070c4c71f8d2/objects \
-H 'Authorization: Bearer gtk_35MCHFgkNh1PymQEOLStzMtESdo4DZXykoYWvjX9QcQ=' \
-H 'Content-Type: multipart/form-data' \
-F 'attr={"Ryanair.pdf":{"refs":{"predecessor":"77384ad2-80a4-11ee-9edc-9747c96a1231","sekatko:vpd":"bd4a3c4a-80a6-11ee-8e34-67217a8a9f66"},"meta":{"sekatko":{"ocr":"sometext"},"otherapp":{"whatever":"https://glued.example.com"}}}};type=application/json' \
-F 'file[]=@/home/killua/Ryanair.pdf' \
-F 'file[]=@/home/killua/eastern loves_INDUSTRA.png' \
-F 'file[]=@/home/killua/todo-petru' \
-F 'field1=fiels2' | jq .
```

```bash
curl -k -X POST https://glued/api/stor/files/v1 \
-H 'Authorization: Bearer gtk_35MCHFgkNh1PymQEOLStzMtESdo4DZXykoYWvjX9QcQ=' \
-H 'Content-Type: multipart/form-data' \
-F 'links={ "Ryanair.pdf": { "uuid": "77384ad2-80a4-11ee-9edc-9747c96a1231", "parent": "bd4a3c4a-80a6-11ee-8e34-67217a8a9f66", "app": { "name": "client-name", "instance": "https://glued.example.com", "discover": "/api/client-name/v1/endpoint/someID" }}, "eastern loves_INDUSTRA.png": {}};type=application/json' \
-F 'file[]=@/home/killua/Ryanair.pdf' \
-F 'file[]=@/home/killua/eastern loves_INDUSTRA.png' \
-F 'file[]=@/home/killua/todo-petru' \
-F 'field1=fiels2'
```



## Metadata
- each file identified by uuid
Expand Down
70 changes: 58 additions & 12 deletions glued/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use mysqli_sql_exception;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
//use Slim\Psr7\Stream;
//use Slim\Psr7\Factory\StreamFactory;
use Nyholm\Psr7\Factory\Psr17Factory;
use Ramsey\Uuid\Uuid;

Expand Down Expand Up @@ -247,8 +245,6 @@ public function dgs($uuid = null, $log = true)
}
}



if ($log == true) {
$q1 = "INSERT INTO `t_stor_dgs` (`uuid`, `data`) VALUES (uuid_to_bin(?, true), ?) ON DUPLICATE KEY UPDATE `data` = VALUES(`data`)";
$q2 = "INSERT IGNORE INTO `t_stor_configlog` (`uuid`, `data`) VALUES (uuid_to_bin(?, true), ?)";
Expand Down Expand Up @@ -600,7 +596,7 @@ public function decodeRetrievalKey(string $token): ?array
}


private function write_object($file, $bucket, $meta): array {
private function write_object($file, $bucket, $meta = null, $refs = null): array {

// Filter writable devices (for now, only filesystem devices are allowed)
foreach ($bucket['devices'] as &$dev) { $dev['path'] = $this->devices($dev['uuid'])['path']; }
Expand Down Expand Up @@ -688,12 +684,47 @@ private function write_object($file, $bucket, $meta): array {

$object['uuid'] = $objUUID;
$object['link'] = $this->generateRetrievalUri($objUUID, $bucket['uuid']);
$object['refs'] = $meta;

if (isset($refs)) { $object['refs'] = $refs; }
if (isset($meta)) { $object['meta'] = $meta; }
return $object;
}

private function patch_object_meta($objUUID, $meta)
{
$q = "
INSERT INTO `t_stor_objects_meta`
(`uuid`, `data`) VALUES (uuid_to_bin(?, 1), ?) ON DUPLICATE KEY UPDATE data = JSON_MERGE_PATCH(data, ?);
";
return $this->mysqli->execute_query($q, [ $objUUID, json_encode($meta), json_encode($meta) ]);
}

private function del_object_refs($objUUID, $refNs, $refKind, $refUUID)
{
if (!$this->is_uuidv4($objUUID) && !$this->is_uuidv4($refUUID) && !is_string($refNs) && !is_string($refKind)) return false;
if ($refNs === '' || $refKind === '') return false;
$q = "
DELETE FROM t_stor_object_refs
WHERE obj = ?
AND ref_ns = ?
AND ref_kind = ?
AND ref_val = ?
";
$this->mysqli->execute_query($q, [ $objUUID, $refNs, $refKind, $refUUID ]);
return true;
}

private function add_object_refs($objUUID, $refNs, $refKind, $refUUID)
{
if (!$this->is_uuidv4($objUUID) && !$this->is_uuidv4($refUUID) && !is_string($refNs) && !is_string($refKind)) return false;
if ($refNs === '' || $refKind === '') return false;
$q = "
INSERT INTO `t_stor_objects_refs` (`obj`, `ref_ns`, `ref_kind`, `ref_val`)
VALUES (uuid_to_bin(?, 1), ?, ?, uuid_to_bin(?, 1))
ON DUPLICATE KEY UPDATE `ref_val` = values(`ref_val`)
";
$this->mysqli->execute_query($q, [ $objUUID, $refNs, $refKind, $refUUID ]);
return true;
}

// TODO t_core_tokens now has only a c_inherit column. we'll need a c_owner column too, because user tokens will use c_inherit, but svc tokens will have c_owner (the one whom is the token management associated to)
public function objects_c1(Request $request, Response $response, array $args = []): Response {
Expand All @@ -705,10 +736,9 @@ public function objects_c1(Request $request, Response $response, array $args = [
400);
}


$bucket = $this->get_buckets($args['bucket']);
if ($bucket == []) { throw new \Exception("Bucket `{$args['bucket']}` not found.", 404); }
$data['bucket'] = $bucket;
//$data['bucket'] = $bucket;

//return $response->withJson($data);
$files = $request->getUploadedFiles();
Expand All @@ -722,6 +752,8 @@ public function objects_c1(Request $request, Response $response, array $args = [
$headerValueString = $request->getHeaderLine('Accept');

$meta = json_decode($parsedBody['meta'] ?? '{}', true);
$refs = json_decode($parsedBody['refs'] ?? '{}', true);


if (!empty($files)) {

Expand All @@ -730,12 +762,25 @@ public function objects_c1(Request $request, Response $response, array $args = [
if (is_array($f)) { foreach ($f as $kk=>$ff) { $flattened[$k.'+'.$kk] = $ff; } }
else { $flattened[$k] = $f; }
}

foreach ($flattened as $file) {
if ($file->getError() === UPLOAD_ERR_OK) {
$res = $this->write_object($file, $bucket, $meta[$file->getClientFilename()] ?? []);
$objMeta = $meta[$file->getClientFilename()] ?? [];
$objRefs = $refs[$file->getClientFilename()] ?? [];
$res = $this->write_object($file, $bucket, $objMeta, $objRefs);
$this->mysqli->begin_transaction();
$this->patch_object_meta($res['uuid'], $objMeta);
foreach ($objRefs as $rk => $refUUIDs) {
$parts = explode(':', $rk, 2);
if (count($parts) == 1) { array_unshift($parts, "_"); }
list($refNs, $refKind) = $parts;
if (is_string($refUUIDs)) { $refUUIDs = [ $refUUIDs ]; }
if (!is_array($refUUIDs)) { throw new \Exception('Ref value must be a UUID or list of UUIDs.'); }
foreach ($refUUIDs as $refUUID) {
$this->add_object_refs($res['uuid'], $refNs, $refKind, $refUUID);
}
}
$this->mysqli->commit();
$data[] = $res;

} else {
// Add error message to response array
$data[] = array(
Expand All @@ -745,6 +790,7 @@ public function objects_c1(Request $request, Response $response, array $args = [
);
}
}

return $response->withJson($data)->withStatus(200);
} else {
return $response->withJson([ 'error' => 'No files were attached to the request.' ])->withStatus(400);
Expand Down

0 comments on commit 238b275

Please sign in to comment.