1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
<?php
namespace JsonApi\Routes\Files;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\BadRequestException;
use JsonApi\Errors\InternalServerError;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\File as FileSchema;
class FileRefsCreate extends JsonApiController
{
use RangeHelperTrait, RoutesHelperTrait, ValidationTrait {
ValidationTrait::arrayGet insteadof RoutesHelperTrait;
ValidationTrait::arrayHas insteadof RoutesHelperTrait;
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameters)
*/
public function __invoke(Request $request, Response $response, $args)
{
$json = $this->validate($request);
// validate parent folder
if (!$parent = \FileManager::getTypedFolder($args['id'])) {
throw new RecordNotFoundException();
}
if (!Authority::canCreateFileRefsInFolder($user = $this->getUser($request), $parent)) {
throw new AuthorizationFailedException();
}
$fileRef = $this->createFileRef($user, $json, $parent);
return $this->getCreatedResponse($fileRef);
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameters)
*/
protected function validateResourceDocument($json, $data)
{
if ($err = $this->validateFileRefResourceObject($json, null)) {
return $err;
}
// Relationship: file
if (self::arrayHas($json, 'data.relationships.file')) {
$file = self::arrayGet($json, 'data.relationships.file');
if ($err = $this->validateResourceIdentifier($file, FileSchema::TYPE, false)) {
return $err;
}
$fileId = self::arrayGet($file, 'data.id');
if (!\File::find($fileId)) {
return 'Invalid `file` specified.';
}
}
}
private function createFileRef(
\User $user,
array $json,
\FolderType $parentFolder
) {
$getTrimmed = function ($key, $default = '') use ($json) {
return trim(self::arrayGet($json, $key, $default));
};
$name = $getTrimmed('data.attributes.name');
$description = $getTrimmed('data.attributes.description');
$licenseId = $getTrimmed('data.relationships.terms-of-use.data.id');
if (self::arrayHas($json, 'data.relationships.file')) {
$fileId = $getTrimmed('data.relationships.file.data.id');
$source = \File::find($fileId);
if ($source->user_id === $user->id) {
$file = $source;
} else {
$file = $this->copyFile($source, $user);
}
} else {
try {
$file = \File::create(
[
'filetype' => 'StandardFile',
'name' => $name,
'size' => 0,
'user_id' => $user->id,
]
);
$emptyTmpFile = $this->getTmpFile();
$file->connectWithDataFile($emptyTmpFile);
} finally {
@unlink($emptyTmpFile);
}
}
$fileRef = new \FileRef();
$fileRef->name = $name;
$fileRef->description = $description;
$fileRef->content_terms_of_use_id = $licenseId;
$filetype = $parentFolder->addFile(
new \StandardFile($fileRef, $file)
);
return $filetype->getFileRef();
}
private function copyFile(\File $source, \User $user)
{
$file = new \File();
$file->user_id = $user->id;
$file->mime_type = $source->mime_type;
$file->name = $source->name;
$file->size = $source->size;
$file->metadata = $source->metadata;
$file->author_name = $user->getFullName('no_title');
$file->id = $file->getNewId();
// We must copy the physical data.
if ($source->getPath()) {
$copyPath = $this->getTmpPath() . '/' . $file->id;
if (!copy($source->getPath(), $copyPath)) {
throw new InternalServerError('Could not copy File.');
}
if ($file->connectWithDataFile($copyPath)) {
@unlink($copyPath);
} else {
throw new InternalServerError('Could not connect file.');
}
} else {
$file->url = $source->url;
}
$file->store();
return $file;
}
}
|