Skip to content

Commit

Permalink
Suport backwards references for blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Jan 23, 2021
1 parent 02172d2 commit 1bc0e79
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"ezyang/htmlpurifier": ">=v4.13.0",
"cboden/ratchet" : "v0.4.3",

"tsugi/lib": "dev-master#ab04db0b9e0a74f27b0e8ed9e084b5913b82dc5f",
"tsugi/lib": "dev-master#068ec64e97285ea2a3460653eedd5cd7b1a33263",
"koseu/lib": "dev-master#5c5bcb32469977bea262b1900461c3f205adb899"
},
"config": {
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -5526,12 +5526,12 @@
"source": {
"type": "git",
"url": "https://github.com/tsugiproject/tsugi-php.git",
"reference": "ab04db0b9e0a74f27b0e8ed9e084b5913b82dc5f"
"reference": "068ec64e97285ea2a3460653eedd5cd7b1a33263"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tsugiproject/tsugi-php/zipball/ab04db0b9e0a74f27b0e8ed9e084b5913b82dc5f",
"reference": "ab04db0b9e0a74f27b0e8ed9e084b5913b82dc5f",
"url": "https://api.github.com/repos/tsugiproject/tsugi-php/zipball/068ec64e97285ea2a3460653eedd5cd7b1a33263",
"reference": "068ec64e97285ea2a3460653eedd5cd7b1a33263",
"shasum": ""
},
"require": {
Expand Down
55 changes: 47 additions & 8 deletions vendor/tsugi/lib/src/Blob/BlobUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ public static function mkdirSha256($sha_256, $blob_root=false /* Unit Test*/)
* Returns false for any number of failures, for better detail, use
* validateUpload() before calling this to do the actual upload.
*/
public static function uploadToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK=true)
public static function uploadToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK=true, $backref=null)
{
$retval = self::uploadFileToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK);
$retval = self::uploadFileToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK, $backref);
if ( is_array($retval) ) $retval = $retval[0];
return $retval;
}
Expand Down Expand Up @@ -202,7 +202,7 @@ public static function isTestKey($key)
* Returns false for any number of failures, for better detail, use
* validateUpload() before calling this to do the actual upload.
*/
public static function uploadFileToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK=true)
public static function uploadFileToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK=true, $backref=null)
{
global $CFG, $CONTEXT, $LINK, $PDOX;

Expand Down Expand Up @@ -268,15 +268,16 @@ public static function uploadFileToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK=true)
// Blob is safe somewhere, insert the file record with pointers
if ( $blob_id || $blob_name ) {
$stmt = $PDOX->prepare("INSERT INTO {$CFG->dbprefix}blob_file
(context_id, link_id, file_sha256, file_name, contenttype, path, blob_id, created_at)
VALUES (:CID, :LID, :SHA, :NAME, :TYPE, :PATH, :BID, NOW())");
(context_id, link_id, file_sha256, file_name, contenttype, path, backref, blob_id, created_at)
VALUES (:CID, :LID, :SHA, :NAME, :TYPE, :PATH, :BACKREF, :BID, NOW())");
$stmt->execute(array(
":CID" => $CONTEXT->id,
":LID" => $LINK->id,
":SHA" => $sha256,
":NAME" => $filename,
":TYPE" => $FILE_DESCRIPTOR['type'],
":PATH" => $blob_name,
":BACKREF" => $backref,
":BID" => $blob_id
));
$id = 0+$PDOX->lastInsertId();
Expand All @@ -290,10 +291,47 @@ public static function uploadFileToBlob($FILE_DESCRIPTOR, $SAFETY_CHECK=true)
return false;
}

/**
* Set the backref for a file entry
*
* This is a soft foreign key in the frorm of:
*
* table:column:value
* peer_submit::submit_id::2
*
* This is uses when we want to link a blob to a record at an even finer
* level than context_id and link_id which is done by default.
*
* When an application is doing a two-phase commit where it
* is uploading the file and then creating the record in the
* table that references the file (i.e. peer_submit in the
* above example), it can set the back ref to have a value
* of -1 to indicate that the file is as yet unlinked
*
* peer_submit::submit_id::-1
*
* and then once the peer_submit record is committed call this routine to
* point to the real record in the table.
*/
public static function setBackref($file_id, $backref)
{
global $CFG, $CONTEXT, $LINK, $PDOX;

$stmt = $PDOX->queryDie("UPDATE {$CFG->dbprefix}blob_file
SET backref=:BACKREF
WHERE file_id = :ID AND context_id=:CID AND link_id = :LID",
array(
":ID" => $file_id,
":CID" => $CONTEXT->id,
":LID" => $LINK->id,
":BACKREF" => $backref,
));
}

/**
* Read a file off local disk and put it into a blob
*/
public static function uploadPathToBlob($filename, $content_type)
public static function uploadPathToBlob($filename, $content_type, $backref=null)
{
global $CFG, $CONTEXT, $LINK, $PDOX;

Expand Down Expand Up @@ -351,15 +389,16 @@ public static function uploadPathToBlob($filename, $content_type)
// Blob is safe somewhere, insert the file record with pointers
if ( $blob_id || $blob_name ) {
$stmt = $PDOX->prepare("INSERT INTO {$CFG->dbprefix}blob_file
(context_id, link_id, file_sha256, file_name, contenttype, path, blob_id, created_at)
VALUES (:CID, :LID, :SHA, :NAME, :TYPE, :PATH, :BID, NOW())");
(context_id, link_id, file_sha256, file_name, contenttype, path, backref, blob_id, created_at)
VALUES (:CID, :LID, :SHA, :NAME, :TYPE, :PATH, :BACKREF, :BID, NOW())");
$stmt->execute(array(
":CID" => $CONTEXT->id,
":LID" => $LINK->id,
":SHA" => $sha256,
":NAME" => $filename,
":TYPE" => $content_type,
":PATH" => $blob_name,
":BACKREF" => $backref,
":BID" => $blob_id
));
$id = 0+$PDOX->lastInsertId();
Expand Down

0 comments on commit 1bc0e79

Please sign in to comment.