Skip to content

Commit fc64ea6

Browse files
committed
Allow to directly set the expireDate on a new (link)share
Since this extends the API we now properly parse the date. We only accept valid ISO 8601 Dates (YYYY-MM-DD). Currently this only works for link shares (it is just ignored for other shares). Since we do not have user/group/federated expiring shares yet. * Tests added
1 parent dc7a4e1 commit fc64ea6

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

apps/files_sharing/api/local.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ public static function createShare($params) {
258258
$itemSource = self::getFileId($path);
259259
$itemSourceName = $itemSource;
260260
$itemType = self::getItemType($path);
261+
$expirationDate = null;
261262

262263
if($itemSource === null) {
263264
return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
@@ -286,6 +287,14 @@ public static function createShare($params) {
286287
// read, create, update (7) if public upload is enabled or
287288
// read (1) if public upload is disabled
288289
$permissions = $publicUpload === 'true' ? 7 : 1;
290+
291+
// Get the expiration date
292+
try {
293+
$expirationDate = isset($_POST['expireDate']) ? self::parseDate($_POST['expireDate']) : null;
294+
} catch (\Exception $e) {
295+
return new \OC_OCS_Result(null, 404, 'Invalid Date');
296+
}
297+
289298
break;
290299
default:
291300
return new \OC_OCS_Result(null, 400, "unknown share type");
@@ -302,8 +311,9 @@ public static function createShare($params) {
302311
$shareType,
303312
$shareWith,
304313
$permissions,
305-
$itemSourceName
306-
);
314+
$itemSourceName,
315+
$expirationDate
316+
);
307317
} catch (HintException $e) {
308318
return new \OC_OCS_Result(null, 400, $e->getHint());
309319
} catch (\Exception $e) {
@@ -537,6 +547,30 @@ public static function deleteShare($params) {
537547
}
538548
}
539549

550+
/**
551+
* Make sure that the passed date is valid ISO 8601
552+
* So YYYY-MM-DD
553+
* If not throw an exception
554+
*
555+
* @param string $expireDate
556+
*
557+
* @throws \Exception
558+
* @return \DateTime
559+
*/
560+
private static function parseDate($expireDate) {
561+
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $expireDate) === 0) {
562+
throw new \Exception();
563+
}
564+
565+
$date = new \DateTime($expireDate);
566+
567+
if ($date === false) {
568+
throw new \Exception();
569+
}
570+
571+
return $date;
572+
}
573+
540574
/**
541575
* get file ID from a given path
542576
* @param string $path

apps/files_sharing/tests/api.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,4 +1487,60 @@ public function testDefaultExpireDate() {
14871487
$config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
14881488

14891489
}
1490+
1491+
public function datesProvider() {
1492+
$date = new \DateTime();
1493+
$date->add(new \DateInterval('P5D'));
1494+
1495+
$year = (int)$date->format('Y');
1496+
1497+
return [
1498+
[$date->format('Y-m-d'), true],
1499+
[$year+1 . '-1-1', false],
1500+
[$date->format('Y-m-dTH:m'), false],
1501+
['abc', false],
1502+
[$date->format('Y-m-d') . 'xyz', false],
1503+
];
1504+
}
1505+
1506+
/**
1507+
* Make sure only ISO 8601 dates are accepted
1508+
*
1509+
* @dataProvider datesProvider
1510+
*/
1511+
public function testPublicLinkExpireDate($date, $valid) {
1512+
$_POST['path'] = $this->folder;
1513+
$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
1514+
$_POST['expireDate'] = $date;
1515+
1516+
$result = \OCA\Files_Sharing\API\Local::createShare([]);
1517+
1518+
if ($valid === false) {
1519+
$this->assertFalse($result->succeeded());
1520+
$this->assertEquals(404, $result->getStatusCode());
1521+
$this->assertEquals('Invalid Date', $result->getMeta()['message']);
1522+
return;
1523+
}
1524+
1525+
$this->assertTrue($result->succeeded());
1526+
1527+
$data = $result->getData();
1528+
$this->assertTrue(is_string($data['token']));
1529+
1530+
// check for correct link
1531+
$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
1532+
$this->assertEquals($url, $data['url']);
1533+
1534+
1535+
$share = $this->getShareFromId($data['id']);
1536+
$items = \OCP\Share::getItemShared('file', $share['item_source']);
1537+
$this->assertTrue(!empty($items));
1538+
1539+
$item = reset($items);
1540+
$this->assertTrue(is_array($item));
1541+
$this->assertEquals($date, substr($item['expiration'], 0, 10));
1542+
1543+
$fileinfo = $this->view->getFileInfo($this->folder);
1544+
\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
1545+
}
14901546
}

0 commit comments

Comments
 (0)