Skip to content

Commit

Permalink
Issue 83: Drop the $mtime variable, introduce IsoDateTime::format().
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Jan 9, 2025
1 parent 23836ab commit b518445
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use Drupal\collabora_online\Jwt\JwtTranscoderInterface;
use Drupal\collabora_online\MediaHelperInterface;
use Drupal\collabora_online\Util\IsoDateTime;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Expand Down Expand Up @@ -73,8 +74,6 @@ public function __construct(
* The response with file contents.
*/
public function wopiCheckFileInfo(MediaInterface $media, FileInterface $file, UserInterface $user, bool $can_write): Response {
$mtime = date_create_immutable_from_format('U', $file->getChangedTime());

if ($can_write && !$media->access('edit in collabora', $user)) {
$this->logger->error('Token and user permissions do not match.');
throw new AccessDeniedHttpException('The user does not have collabora edit access for this media.');
Expand All @@ -83,7 +82,7 @@ public function wopiCheckFileInfo(MediaInterface $media, FileInterface $file, Us
$response_data = [
'BaseFileName' => $file->getFilename(),
'Size' => $file->getSize(),
'LastModifiedTime' => $mtime->format('c'),
'LastModifiedTime' => IsoDateTime::format($file->getChangedTime()),
'UserId' => $user->id(),
'UserFriendlyName' => $user->getDisplayName(),
'UserCanWrite' => $can_write,
Expand Down Expand Up @@ -149,7 +148,6 @@ public function wopiPutFile(MediaInterface $media, FileInterface $file, UserInte

$new_file_content = $request->getContent();
$new_file = $this->createNewFileEntity($file, $new_file_content);
$mtime = date_create_immutable_from_format('U', $new_file->getChangedTime());

$save_reason = $this->buildSaveReason($request);

Expand All @@ -163,7 +161,7 @@ public function wopiPutFile(MediaInterface $media, FileInterface $file, UserInte

return new JsonResponse(
[
'LastModifiedTime' => $mtime->format('c'),
'LastModifiedTime' => IsoDateTime::format($new_file->getChangedTime()),
],
Response::HTTP_OK,
['content-type' => 'application/json'],
Expand Down
27 changes: 27 additions & 0 deletions src/Util/IsoDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Drupal\collabora_online\Util;

/**
* Static methods related to date and time.
*/
class IsoDateTime {

/**
* Formats a timestamp as a date in UTC timezone in ISO 8601 format.
*
* @param int $timestamp
* Unix timestamp.
*
* @return string
* Formatted date string in ISO 8601 format, in UTC.
*/
public static function format(int $timestamp): string {
// If the input is in timestamp format, the timezone is always UTC.
return \DateTimeImmutable::createFromFormat('U', (string) $timestamp)
->format('c');
}

}

0 comments on commit b518445

Please sign in to comment.