richdocuments/lib/Helper.php

72 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2013-08-18 19:02:48 +03:00
<?php
/**
2015-12-16 17:57:44 +03:00
* ownCloud - Richdocuments App
2013-08-18 19:02:48 +03:00
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
2015-12-16 17:57:44 +03:00
namespace OCA\Richdocuments;
2013-08-18 19:02:48 +03:00
use \DateTime;
use \DateTimeZone;
2014-02-12 15:46:53 +03:00
class Helper {
2015-12-16 17:57:44 +03:00
const APP_ID = 'richdocuments';
2014-02-12 15:46:53 +03:00
2017-05-11 21:01:40 +05:30
/**
* @param string $fileId
* @return array
* @throws \Exception
*/
public static function parseFileId($fileId) {
$arr = explode('_', $fileId);
if (count($arr) === 1) {
$fileId = $arr[0];
$instanceId = '';
2017-05-11 21:01:40 +05:30
$version = '0';
} else if (count($arr) === 2) {
list($fileId, $instanceId) = $arr;
$version = '0';
} else if (count($arr) === 3) {
list($fileId, $instanceId, $version) = $arr;
} else {
throw new \Exception('$fileId has not the expected format');
}
return [
$fileId,
$instanceId,
$version,
];
}
/**
* WOPI helper function to convert to ISO 8601 round-trip format.
* @param integer $time Must be seconds since unix epoch
*/
public static function toISO8601($time)
{
// TODO: Be more precise and don't ignore milli, micro seconds ?
$datetime = DateTime::createFromFormat('U', $time, new DateTimeZone('UTC'));
if ($datetime)
return $datetime->format('Y-m-d\TH:i:s.u\Z');
return false;
}
2017-05-11 21:01:40 +05:30
public static function getNewFileName($view, $path, $prepend = ' '){
$fileNum = 1;
2014-02-12 15:46:53 +03:00
while ($view->file_exists($path)){
$fileNum += 1;
$path = preg_replace('/(\.|' . $prepend . '\(\d+\)\.)([^.]*)$/', $prepend . '(' . $fileNum . ').$2', $path);
};
2014-02-12 15:46:53 +03:00
return $path;
}
2014-02-12 15:46:53 +03:00
}