Skip to content

Commit

Permalink
update file
Browse files Browse the repository at this point in the history
  • Loading branch information
kongtiaowang committed May 21, 2019
1 parent 1b41002 commit f7a9519
Showing 1 changed file with 5 additions and 53 deletions.
58 changes: 5 additions & 53 deletions modules/document_repository/php/files.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ class Files extends \NDB_Page
}
break;
case "PUT":
$this->editDocFile();
$tmp = $request->getBody();
$arr = (array)json_decode($tmp);
$this->editDocFile($arr);
return (new \LORIS\Http\Response())
->withHeader("Content-Type", "text/plain")
->withStatus(200)
Expand Down Expand Up @@ -144,16 +146,15 @@ class Files extends \NDB_Page
/**
* Handles the document editing process
*
* @param array $req the request
* @throws \DatabaseException
*
* @return void
*/
function editDocFile(): void
function editDocFile(array $req): void
{
$db = \Database::singleton();
$user = \User::singleton();
$req = array();
$this->parseRawHttpRequest($req);
$updateValues = [
'instrument' => $req['instrument'] ?? null,
'comments' => $req['comments'] ?? null,
Expand All @@ -171,55 +172,6 @@ class Files extends \NDB_Page
showError("Could not update the file. Please try again!");
}
}
/**
* Handles the raw form-data from PUT method to an array.
*
* @param array $a_data an init array
*
* @throws \DatabaseException
* @return void
*/
function parseRawHttpRequest(array &$a_data): void
{
// read incoming data
$input = file_get_contents('php://input');
// grab multipart boundary from content type header
preg_match('/boundary=(.*)$/', $_SERVER['CONTENT_TYPE'], $matches);
$boundary = $matches[1];

// split content by boundary and get rid of last -- element
$a_blocks = preg_split("/-+$boundary/", $input);
array_pop($a_blocks);

// loop data blocks
foreach ($a_blocks as $id => $block) {
if (empty($block)) {
continue;
}

// you'll have to var_dump $block to understand this and maybe replace
// \n or \r with a visibile char

// parse uploaded files
if (strpos($block, 'application/octet-stream') !== false) {
// match "name", then everything after "stream" (optional) except
// for prepending newlines
preg_match(
"/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s",
$block,
$matches
);
} else {
// match "name" and optional value in between newline sequences
preg_match(
'/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s',
$block,
$matches
);
}
$a_data[$matches[1]] = $matches[2];
}
}
/**
* Handles the document deleting process
*
Expand Down

0 comments on commit f7a9519

Please sign in to comment.