Add mime type get to the Check\File class
This commit is contained in:
@@ -28,10 +28,10 @@ final class CoreLibsCheckFileTest extends TestCase
|
|||||||
public function filesList(): array
|
public function filesList(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
['filename.txt', 'txt', 5],
|
['filename.txt', 'txt', 5, 'text/plain'],
|
||||||
['filename.csv', 'csv', 15],
|
['filename.csv', 'csv', 15, 'text/csv'],
|
||||||
['filename.tsv', 'tsv', 0],
|
['filename.tsv', 'tsv', 0, 'text/plain'],
|
||||||
['file_does_not_exits', '', -1],
|
['file_does_not_exits', '', -1, ''],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +63,15 @@ final class CoreLibsCheckFileTest extends TestCase
|
|||||||
return $list;
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function mimeTypeProvider(): array
|
||||||
|
{
|
||||||
|
$list = [];
|
||||||
|
foreach ($this->filesList() as $row) {
|
||||||
|
$list[$row[0] . ' must be mime type ' . $row[3]] = [$row[0], $row[3]];
|
||||||
|
}
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests if file extension matches
|
* Tests if file extension matches
|
||||||
*
|
*
|
||||||
@@ -115,6 +124,51 @@ final class CoreLibsCheckFileTest extends TestCase
|
|||||||
unlink($this->base_folder . $input);
|
unlink($this->base_folder . $input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @covers ::getMimeType
|
||||||
|
* @dataProvider mimeTypeProvider
|
||||||
|
* @testdox getMimeType $input must be mime type $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param string $input
|
||||||
|
* @param string $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testGetMimeType(string $input, string $expected): void
|
||||||
|
{
|
||||||
|
if (!empty($expected)) {
|
||||||
|
$file = $this->base_folder . $input;
|
||||||
|
$fp = fopen($file, 'w');
|
||||||
|
switch ($expected) {
|
||||||
|
case 'text/csv':
|
||||||
|
for ($i = 1; $i <= 10; $i++) {
|
||||||
|
fwrite($fp, '"This is row","' . $expected . '",' . $i . PHP_EOL);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'text/tsv':
|
||||||
|
for ($i = 1; $i <= 10; $i++) {
|
||||||
|
fwrite($fp, "\"This is row\"\t\"" . $expected . "\"\t\"" . $i . PHP_EOL);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'text/plain':
|
||||||
|
fwrite($fp, 'This is mime type: ' . $expected . PHP_EOL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fclose($fp);
|
||||||
|
} else {
|
||||||
|
$this->expectException(\UnexpectedValueException::class);
|
||||||
|
}
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Check\File::getMimeType($this->base_folder . $input)
|
||||||
|
);
|
||||||
|
// unlink file
|
||||||
|
if (is_file($this->base_folder . $input)) {
|
||||||
|
unlink($this->base_folder . $input);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ $file = '/some/path/to/some/file.txt';
|
|||||||
print "GETFILENAMEENDING: $file: " . File::getFilenameEnding($file) . "<br>";
|
print "GETFILENAMEENDING: $file: " . File::getFilenameEnding($file) . "<br>";
|
||||||
$file = getcwd() . DIRECTORY_SEPARATOR . 'class_test.file.php';
|
$file = getcwd() . DIRECTORY_SEPARATOR . 'class_test.file.php';
|
||||||
print "GETLINESFROMFILE: $file: " . File::getLinesFromFile($file) . "<br>";
|
print "GETLINESFROMFILE: $file: " . File::getLinesFromFile($file) . "<br>";
|
||||||
|
print "MIMEINFO: $file: " . File::getMimeType($file) . "<br>";
|
||||||
|
|
||||||
print "</body></html>";
|
print "</body></html>";
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,23 @@ class File
|
|||||||
// return lines in file
|
// return lines in file
|
||||||
return $lines;
|
return $lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the mime type of a file via finfo
|
||||||
|
* if file not found, throws exception
|
||||||
|
* else returns '' for any other finfo read problem
|
||||||
|
*
|
||||||
|
* @param string $read_file File to read, relative or absolute path
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getMimeType(string $read_file): string
|
||||||
|
{
|
||||||
|
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
||||||
|
if (!is_file($read_file)) {
|
||||||
|
throw new \UnexpectedValueException('[getMimeType] File not found: ' . $read_file);
|
||||||
|
}
|
||||||
|
return $finfo->file($read_file) ?: '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
Reference in New Issue
Block a user