-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessageDocumentStorage.php
149 lines (125 loc) · 4.61 KB
/
MessageDocumentStorage.php
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
148
149
<?php
// This file is part of Bileto.
// Copyright 2022-2025 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace App\Service;
use App\Entity\MessageDocument;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\File;
/**
* This class provides methods to save and read files as MessageDocuments.
*
* @see docs/developers/document-upload.md
*/
class MessageDocumentStorage
{
private const HASH_ALGO = 'sha256';
public function __construct(
#[Autowire(param: 'app.uploads_directory')]
public readonly string $uploadsDirectory,
) {
}
/**
* Store a Symfony File with the given name and return a corresponding
* MessageDocument.
*
* If the file already exists in the storage, it isn't duplicated, but a
* new MessageDocument is initialized with the same filename.
*
* @throws MessageDocumentStorageError
* If the file cannot be saved (e.g. invalid mimetype, file doesn't
* exist, cannot move the file to its destination).
*/
public function store(File $file, string $name, bool $copy = false, bool $trustMimeType = false): MessageDocument
{
$pathname = $file->getPathname();
try {
$mimetype = $file->getMimeType();
} catch (\Exception $e) {
throw MessageDocumentStorageError::unreadableFile($pathname);
}
if (!$trustMimeType && !MessageDocument::isMimetypeAccepted($mimetype)) {
throw MessageDocumentStorageError::rejectedMimetype($mimetype);
}
$hash = @hash_file(self::HASH_ALGO, $pathname);
if ($hash === false) {
throw MessageDocumentStorageError::unreadableFile($pathname);
}
// Calculate a filename based on the hash of its content so we can
// easily avoid to store the same file twice.
$filename = "{$hash}.{$file->guessExtension()}";
$messageDocument = new MessageDocument();
$messageDocument->setName($name);
$messageDocument->setFilename($filename);
$messageDocument->setMimetype($mimetype);
$messageDocument->setHash(self::HASH_ALGO, $hash);
$pathname = $this->getPathname($messageDocument);
if (!file_exists($pathname)) {
$directory = "{$this->uploadsDirectory}/{$messageDocument->getFilepath()}";
if ($copy) {
if (!is_dir($directory)) {
mkdir($directory, 0755, recursive: true);
}
copy($file->getPathname(), "{$directory}/{$filename}");
} else {
try {
$file->move($directory, $messageDocument->getFilename());
} catch (FileException $e) {
throw MessageDocumentStorageError::immovableFile($pathname, $directory, $e->getMessage());
}
}
}
return $messageDocument;
}
/**
* Return whether the file exists or not.
*/
public function exists(MessageDocument $messageDocument): bool
{
$pathname = $this->getPathname($messageDocument);
return @file_exists($pathname);
}
/**
* Return the size of the file related to the given MessageDocument.
*
* @throws MessageDocumentStorageError
* If the file doesn't exist.
*/
public function size(MessageDocument $messageDocument): int
{
$pathname = $this->getPathname($messageDocument);
$filesize = @filesize($pathname);
if ($filesize === false) {
throw MessageDocumentStorageError::unreadableFile($pathname);
}
return $filesize;
}
/**
* Return the content of the file related to the given MessageDocument.
*
* @throws MessageDocumentStorageError
* If the file doesn't exist.
*/
public function read(MessageDocument $messageDocument): string
{
$pathname = $this->getPathname($messageDocument);
$content = @file_get_contents($pathname);
if ($content === false) {
throw MessageDocumentStorageError::unreadableFile($pathname);
}
return $content;
}
/**
* Remove the file related to the given MessageDocument.
*/
public function remove(MessageDocument $messageDocument): bool
{
$pathname = $this->getPathname($messageDocument);
return @unlink($pathname);
}
public function getPathname(MessageDocument $messageDocument): string
{
return "{$this->uploadsDirectory}/{$messageDocument->getPathname()}";
}
}