MCPcopy Create free account
hub / github.com/error311/FileRise / createAuthFileLink

Method createAuthFileLink

src/FileRise/Domain/FileModel.php:2880–2939  ·  view source on GitHub ↗
(
        string $folder,
        string $file,
        string $sourceId = '',
        string $createdBy = '',
        ?int $expiresAt = null
    )

Source from the content-addressed store, hash-verified

2878 }
2879
2880 public static function createAuthFileLink(
2881 string $folder,
2882 string $file,
2883 string $sourceId = '',
2884 string $createdBy = '',
2885 ?int $expiresAt = null
2886 ): array {
2887 if (strtolower($folder) !== 'root' && !preg_match(REGEX_FOLDER_NAME, $folder)) {
2888 return ['error' => 'Invalid folder name.'];
2889 }
2890
2891 $file = basename(trim($file));
2892 if (!preg_match(REGEX_FILE_NAME, $file)) {
2893 return ['error' => 'Invalid file name.'];
2894 }
2895
2896 $sourceId = trim($sourceId);
2897 if ($sourceId !== '' && !preg_match('/^[A-Za-z0-9_-]{1,64}$/', $sourceId)) {
2898 $sourceId = '';
2899 }
2900
2901 $expiresAtInt = $expiresAt ? (int)$expiresAt : 0;
2902 if ($expiresAtInt < 0) {
2903 $expiresAtInt = 0;
2904 }
2905
2906 $createdBy = trim($createdBy);
2907 $createdBy = preg_replace('/[\x00-\x1F\x7F]/', '', $createdBy);
2908 $createdAt = time();
2909 $token = bin2hex(random_bytes(32));
2910
2911 $path = self::authFileLinksPathForMetaRoot(self::metaRoot());
2912 $links = self::readAuthFileLinks($path);
2913 $links = self::cleanAuthFileLinks($links, $createdAt);
2914 while (isset($links[$token])) {
2915 $token = bin2hex(random_bytes(32));
2916 }
2917
2918 $record = [
2919 'folder' => $folder,
2920 'file' => $file,
2921 'path' => ($folder === 'root') ? $file : ($folder . '/' . $file),
2922 'sourceId' => $sourceId,
2923 'createdBy' => is_string($createdBy) ? $createdBy : '',
2924 'createdAt' => $createdAt,
2925 ];
2926 if ($expiresAtInt > 0) {
2927 $record['expiresAt'] = $expiresAtInt;
2928 }
2929 $links[$token] = $record;
2930
2931 if (file_put_contents($path, json_encode($links, JSON_PRETTY_PRINT), LOCK_EX) === false) {
2932 return ['error' => 'Could not save auth file link.'];
2933 }
2934
2935 return [
2936 'token' => $token,
2937 'expiresAt' => $expiresAtInt > 0 ? $expiresAtInt : null,

Callers 1

Calls 4

metaRootMethod · 0.95
readAuthFileLinksMethod · 0.95
cleanAuthFileLinksMethod · 0.95

Tested by

no test coverage detected