Skip to content

Commit 140ac02

Browse files
committed
added src
1 parent da2ae48 commit 140ac02

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "trihydera/file",
3+
"description": "File stuff",
4+
"type": "library",
5+
"license": "GPL-2.0-only",
6+
"autoload": {
7+
"psr-4": {
8+
"Trihydera\\File\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "TriHydera",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"minimum-stability": "stable",
18+
"require": {}
19+
}

src/FileInfo.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace TriHydera\File;
3+
4+
class FileInfo {
5+
private $dir;
6+
7+
public function __construct($dir) {
8+
$this->dir = $dir;
9+
}
10+
11+
public function getInfo() {
12+
$details = [];
13+
$this->listFilesRecursively($this->dir, $details);
14+
return $details;
15+
}
16+
17+
private function listFilesRecursively($dir, &$details) {
18+
$files = scandir($dir);
19+
20+
foreach ($files as $file) {
21+
if ($file === '.' || $file === '..') {
22+
continue;
23+
}
24+
25+
$path = $dir . '/' . $file;
26+
27+
if (is_dir($path)) {
28+
$this->listFilesRecursively($path, $details);
29+
} else {
30+
$hash = md5_file($path);
31+
$size = filesize($path);
32+
$modified = filemtime($path);
33+
$formattedSize = $this->formatSize($size);
34+
$shortCode = substr($hash, 2, 4).substr($hash, 10, 4);
35+
36+
$details[$file] = [
37+
'id' => $shortCode,
38+
'name' => $file,
39+
'modified' => date('Y-m-d H:i', $modified),
40+
'size_raw' => $size,
41+
'size' => $formattedSize,
42+
'hash' => $hash,
43+
'path' => $path
44+
];
45+
}
46+
}
47+
}
48+
49+
private function formatSize($bytes) {
50+
$units = array('B', 'KB', 'MB', 'GB', 'TB');
51+
$i = floor(log($bytes, 1024));
52+
return @round($bytes / pow(1024, $i), 2) . ' ' . $units[$i];
53+
}
54+
}
55+
?>

src/FileTypes.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace TriHydera\File;
3+
4+
class FileTypes {
5+
public $types = [
6+
'exe' => ['category' => 'Windows', 'purpose' => 'Executable file'],
7+
'msi' => ['category' => 'Windows', 'purpose' => 'Windows Installer Package'],
8+
'jpg' => ['category' => 'Image', 'purpose' => 'JPEG Image'],
9+
'png' => ['category' => 'Image', 'purpose' => 'PNG Image'],
10+
'txt' => ['category' => 'Text', 'purpose' => 'Plain Text File'],
11+
'pdf' => ['category' => 'Document', 'purpose' => 'PDF Document'],
12+
'doc' => ['category' => 'Document', 'purpose' => 'Microsoft Word Document'],
13+
'xlsx' => ['category' => 'Document', 'purpose' => 'Microsoft Excel Spreadsheet'],
14+
'zip' => ['category' => 'Archive', 'purpose' => 'Compressed Archive'],
15+
'mp3' => ['category' => 'Audio', 'purpose' => 'MP3 Audio File']
16+
];
17+
}
18+
?>

src/JsonFile.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace TriHydera\File;
3+
4+
class JsonFile {
5+
private $log;
6+
7+
public function __construct() {
8+
$this->log = $log;
9+
}
10+
11+
public function read(string $file)
12+
{
13+
$split = explode('/..', $file);
14+
15+
$contents = file_exists($file) ? file_get_contents($file) : '[]';
16+
return json_decode($contents, true);
17+
}
18+
19+
public function write(string $file, array $data)
20+
{
21+
$split = explode('/..', $file);
22+
23+
file_put_contents($file, json_encode($data));
24+
}
25+
}
26+
27+
?>

0 commit comments

Comments
 (0)