Commit b8b1337d authored by Guillaume Heurtier's avatar Guillaume Heurtier
Browse files

FEAT #10001 TIME 3:25 delete old temporary files when sending document to...

FEAT #10001 TIME 3:25 delete old temporary files when sending document to sharepoint + log delete error
parent abfe61a9
......@@ -706,6 +706,6 @@ $app->put('/plugins/outlook/attachments', \Outlook\controllers\OutlookController
$app->post('/office365', \ContentManagement\controllers\Office365SharepointController::class . ':sendDocument');
$app->get('/office365/{id}', \ContentManagement\controllers\Office365SharepointController::class . ':getFileContent');
$app->delete('/office365/{id}', \ContentManagement\controllers\Office365SharepointController::class . ':deleteFile');
$app->delete('/office365/{id}', \ContentManagement\controllers\Office365SharepointController::class . ':delete');
$app->run();
......@@ -23,6 +23,7 @@ use Resource\models\ResModel;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\controllers\LogsController;
use SrcCore\models\CurlModel;
use SrcCore\models\ValidatorModel;
use User\models\UserModel;
......@@ -149,13 +150,13 @@ class Office365SharepointController
$id = $sendResult['response']['id'];
$body = json_encode(['item' => ['@microsoft.graph.conflictBehavior' => 'replace']]);
$requestBody = json_encode(['item' => ['@microsoft.graph.conflictBehavior' => 'replace']]);
$sendResult = CurlModel::exec([
'url' => 'https://graph.microsoft.com/v1.0/sites/' . $configuration['siteId'] . '/drive/items/' . $id . '/createUploadSession',
'bearerAuth' => ['token' => $accessToken],
'headers' => ['Content-Type: application/json', 'Content-Length: ' . strlen($body)],
'headers' => ['Content-Type: application/json', 'Content-Length: ' . strlen($requestBody)],
'method' => 'POST',
'body' => $body
'body' => $requestBody
]);
if ($sendResult['code'] != 200) {
......@@ -179,7 +180,7 @@ class Office365SharepointController
$currentUser = UserModel::getById(['id' => $GLOBALS['id'], 'select' => ['mail']]);
$body = [
$requestBody = [
'requireSignIn' => true,
'sendInvitation' => false,
'roles' => ['read', 'write'],
......@@ -194,13 +195,43 @@ class Office365SharepointController
'bearerAuth' => ['token' => $accessToken],
'headers' => ['Content-Type: application/json'],
'method' => 'POST',
'body' => json_encode($body)
'body' => json_encode($requestBody)
]);
if ($result['code'] != 200) {
return $response->withStatus(400)->withJson(['errors' => 'Could not share the document with user', 'sharepointError' => $result['response']['error']]);
}
// Delete all the other files created by the user, if there are any
$result = CurlModel::exec([
'url' => 'https://graph.microsoft.com/v1.0/sites/' . $configuration['siteId'] . '/drive/root/children',
'bearerAuth' => ['token' => $accessToken],
'method' => 'GET'
]);
if ($result['code'] == 200) {
foreach ($result['response']['value'] as $child) {
if ($child['id'] != $id && strpos($child['name'], 'maarch_' . $GLOBALS['login']) !== false) {
Office365SharepointController::deleteFile([
'configuration' => $configuration,
'token' => $accessToken,
'documentId' => $child['id'],
'resId' => $body['resId']
]);
}
}
} else {
LogsController::add([
'isTech' => true,
'moduleId' => 'resource',
'level' => 'ERROR',
'tableName' => 'letterbox_coll',
'recordId' => $args['resId'],
'eventType' => "Failed to get document list " . json_encode($result['response']['error']),
'eventId' => "resId : {$args['resId']}"
]);
}
return $response->withJson(['webUrl' => $webUrl, 'documentId' => $id]);
}
......@@ -245,7 +276,7 @@ class Office365SharepointController
return $response->withJson(['content' => base64_encode($content)]);
}
public function deleteFile(Request $request, Response $response, array $args)
public function delete(Request $request, Response $response, array $args)
{
$configuration = ConfigurationModel::getByPrivilege(['privilege' => 'admin_document_editors', 'select' => ['value']]);
$configuration = !empty($configuration['value']) ? json_decode($configuration['value'], true) : [];
......@@ -259,21 +290,18 @@ class Office365SharepointController
return $response->withStatus(400)->withJson(['errors' => 'Argument id is empty or not a string']);
}
$body = $request->getParsedBody();
if (!Validator::intVal()->notEmpty()->validate($body['resId'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body resId is empty or not an integer']);
}
$accessToken = Office365SharepointController::getAuthenticationToken(['configuration' => $configuration]);
if (!empty($accessToken['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $accessToken['errors']]);
}
$result = CurlModel::exec([
'url' => 'https://graph.microsoft.com/v1.0/sites/' . $configuration['siteId'] . '/drive/items/' . $args['id'],
'bearerAuth' => ['token' => $accessToken],
'method' => 'DELETE'
]);
if ($result['code'] != 204) {
return $response->withStatus(400)->withJson(['errors' => 'Could not delete document in Sharepoint', 'sharepointError' => $result['response']['error']]);
}
Office365SharepointController::deleteFile(['configuration' => $configuration, 'token' => $accessToken, 'documentId' => $args['id'], 'resId' => $body['resId']]);
return $response->withStatus(204);
}
......@@ -350,4 +378,32 @@ class Office365SharepointController
return $curlResponse['response']['access_token'];
}
private static function deleteFile(array $args)
{
ValidatorModel::notEmpty($args, ['configuration', 'token', 'documentId', 'resId']);
ValidatorModel::stringType($args, ['token', 'documentId']);
ValidatorModel::arrayType($args, ['configuration']);
ValidatorModel::intVal($args, ['resId']);
$configuration = $args['configuration'];
$result = CurlModel::exec([
'url' => 'https://graph.microsoft.com/v1.0/sites/' . $configuration['siteId'] . '/drive/items/' . $args['documentId'],
'bearerAuth' => ['token' => $args['token']],
'method' => 'DELETE'
]);
if ($result['code'] != 204) {
LogsController::add([
'isTech' => true,
'moduleId' => 'resource',
'level' => 'ERROR',
'tableName' => 'letterbox_coll',
'recordId' => $args['resId'],
'eventType' => "Failed to delete document {$args['documentId']} : " . json_encode($result['response']['error']),
'eventId' => "resId : {$args['resId']}"
]);
}
}
}
......@@ -172,7 +172,7 @@ export class Office365SharepointViewerComponent implements OnInit, AfterViewInit
deleteDocument() {
return new Promise((resolve) => {
this.http.delete('../rest/office365/' + this.documentId).pipe(
this.http.request('DELETE', '../rest/office365/' + this.documentId, {body: {resId: this.params.objectId}}).pipe(
tap(() => {
resolve(true);
}),
......
......@@ -2488,7 +2488,7 @@
"redirectedFromProfile": "Les bannettes <b>actuellement redirigées</b> seront redirigées par défaut",
"systemTechnicalFields": "Champs techniques \"Système\"",
"customTechnicalFields": "Champs techniques personnalisés",
"getDocument": "Récupérer le document",
"getDocument": "Valider l'édition",
"office365SharepointNotEnabled": "L'édition avec Office 365 via Sharepoint Online n'est pas activé dans <b>Administration</b> > <b>Paramètres</b> > <b>Autres</b>",
"documentEditor_office365sharepoint": "Utilise la suite Office 365 en éditant les documents dans un espace Sharepoint Online",
"officeSharepointEditDenied": "ne peut pas être édité avec Office 365",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment