[Scummvm-git-logs] scummvm-web master -> 453e74263bc4ea772236d8be18f808aeb8ed5b36
Thunderforge
noreply at scummvm.org
Mon Jan 10 21:39:05 UTC 2022
This automated email contains information about 1 new commit which have been
pushed to the 'scummvm-web' repo located at https://github.com/scummvm/scummvm-web .
Summary:
453e74263b WEB: Adding file size and extension to recommended downloads (#191)
Commit: 453e74263bc4ea772236d8be18f808aeb8ed5b36
https://github.com/scummvm/scummvm-web/commit/453e74263bc4ea772236d8be18f808aeb8ed5b36
Author: Thunderforge (wjherrmann at gmail.com)
Date: 2022-01-10T15:39:01-06:00
Commit Message:
WEB: Adding file size and extension to recommended downloads (#191)
* WEB: Refactor reading files into FileUtils
Refactoring code in `include/Objects/File` into `FileUtils`. This not only makes the code much cleaner, but also allows it to be used in other places of the app, such as the `DownloadsModel`.
* WEB: Adding file size and extension to recommended downloads
Changed paths:
A include/FileUtils.php
include/Models/DownloadsModel.php
include/Objects/File.php
diff --git a/include/FileUtils.php b/include/FileUtils.php
new file mode 100644
index 00000000..c87e927d
--- /dev/null
+++ b/include/FileUtils.php
@@ -0,0 +1,118 @@
+<?php
+namespace ScummVM;
+
+/**
+* Utility functions related to files on a file system.
+*/
+class FileUtils
+{
+ /**
+ * Returns whether or not the file exists and is readable
+ *
+ * @param $path the path to the file that will be analyzed
+ * @return whether or not the file exists
+ */
+ public static function exists($path)
+ {
+ $path = FileUtils::toAbsolutePathIfOnServer($path);
+ return is_file($path) && is_readable($path);
+ }
+
+ /**
+ * Returns the file size in a human-readable form (e.g. 75.2M).
+ *
+ * @param $path the path to the file that will be analyzed
+ * @return the file size in a human-readable form
+ */
+ public static function getFileSize($path)
+ {
+ $path = FileUtils::toAbsolutePathIfOnServer($path);
+ // Get the file size, rounded to the nearest kilobyte
+ $file_size = round((@filesize($path) / 1024));
+
+ if ($file_size < 1024) {
+ $file_size = $file_size . "K";
+ } else {
+ $file_size /= 1024;
+
+ if ($file_size < 1024) {
+ $file_size = round($file_size, 1) . "M";
+ } else {
+ $file_size /= 1024;
+ $file_size = round($file_size, 2) . "G";
+ }
+ }
+ return $file_size;
+ }
+
+ /**
+ * Returns the extension of the given file.
+ *
+ * @param $path the path to the file that will be analyzed
+ * @return the extension
+ */
+ public static function getExtension($path)
+ {
+ $path = FileUtils::toAbsolutePathIfOnServer($path);
+ // Get everything to the right of the last period
+ $extension = substr($path, (strrpos($path, '.')));
+
+ // For certain extensions, check for another extension (e.g. foo.tar.gz => tar.gz)
+ if ($extension == '.bz2' || $extension == '.gz' || $extension == '.xz' || $extension == '.7z') {
+ $extension = substr($path, strrpos($path, '.', -(strlen($path) - strrpos($path, '.') + 1)));
+ }
+ return $extension;
+ }
+
+ /**
+ * Returns the SHA-256 hash of the given file.
+ *
+ * @param $path the path to the file that will be analyzed
+ * @return the SHA-256 hash
+ */
+ public static function getSha256($path)
+ {
+ $path = FileUtils::toAbsolutePathIfOnServer($path);
+ // Check if we already have a generated hash file
+ if ((is_file($path . '.sha256') && is_readable($path . '.sha256'))
+ && (@filemtime($path . '.sha256') > @filemtime($path))
+ ) {
+ // Read the file and return the included hash
+ return file_get_contents($path . '.sha256');
+ } else {
+ // Generate a SHA-256 hash, save it to a file for later, then return the hash
+ $hash = hash_file('sha256', $path);
+ file_put_contents($path . '.sha256', $hash);
+ return $hash;
+ }
+ }
+
+ /**
+ * Returns the date and time that the given file was last modified.
+ *
+ * @param $path the path to the file that will be analyzed
+ * @return the date and time
+ */
+ public static function getLastModified($path)
+ {
+ $path = FileUtils::toAbsolutePathIfOnServer($path);
+ return date('F j, Y, g:i a', @filemtime($path));
+ }
+
+ /**
+ * Returns the path of the file. If the file exists on the ScummVM web server at the expected location, convert to an
+ * absolute path. Otherwise, return the relative path provided as input.
+ *
+ * This method was created due to different behaviors between local developers' machines and the ScummVM server.
+ * For instance, code for calculating SHA-256 hashes would work locally for files in `public_html/frs`, but not on
+ * the ScummVM web server where an absolute path was the only way that the code would work. Using this method means
+ * that code will work in both environments.
+ *
+ * @param $path the relative path to the file that will be analyzed
+ * @return the path of the file, either relative or absolute
+ */
+ private static function toAbsolutePathIfOnServer($relativePath)
+ {
+ return is_file(DIR_SERVER_ROOT . '/'. $relativePath) ? DIR_SERVER_ROOT . '/'. $relativePath : $relativePath;
+ }
+}
diff --git a/include/Models/DownloadsModel.php b/include/Models/DownloadsModel.php
index 08c5ad79..ab5c886f 100644
--- a/include/Models/DownloadsModel.php
+++ b/include/Models/DownloadsModel.php
@@ -2,6 +2,7 @@
namespace ScummVM\Models;
+use ScummVM\FileUtils;
use ScummVM\Objects\DownloadsSection;
use DeviceDetector\Parser\OperatingSystem as OsParser;
use ScummVM\OrmObjects\DownloadQuery;
@@ -115,15 +116,11 @@ class DownloadsModel extends BasicModel
// Construct the URL and fill in the version
$url = DOWNLOADS_BASE . "/" . DOWNLOADS_URL . $download->getURL();
$version = $download->getVersion();
- $url = str_replace('{$version}', $version, $url);
- }
-
- $data = ""; //$download->getExtraInfo();
- if (is_array($data)) {
- $extra_text = $data['size'] . " ";
- $extra_text .= $data['ext'] . " " . $data['msg'];
- } else {
- $extra_text = $data;
+ $file_name = str_replace('{$version}', $version, DOWNLOADS_URL . $download->getURL());
+ $url = DOWNLOADS_BASE . $file_name;
+ if (FileUtils::exists($file_name)) {
+ $extra_text = FileUtils::getFileSize($file_name) . " " . FileUtils::getExtension($file_name);
+ }
}
/*
@@ -143,7 +140,7 @@ class DownloadsModel extends BasicModel
return [
'os' => $name,
'ver' => $version,
- 'desc' => $extra_text,
+ 'desc' => isset($extra_text) ? $extra_text : "",
'url' => $url,
];
}
diff --git a/include/Objects/File.php b/include/Objects/File.php
index 8b19e86d..64b37b33 100644
--- a/include/Objects/File.php
+++ b/include/Objects/File.php
@@ -1,6 +1,8 @@
<?php
namespace ScummVM\Objects;
+use ScummVM\FileUtils;
+
/**
* The File object represents a file on the website.
*/
@@ -44,43 +46,12 @@ class File extends BasicObject
}
$fname = str_replace('{$version}', "$this->version", $fname);
- // Use an absolute file path if available, otherwise use the relative path
- $path = is_file(DIR_SERVER_ROOT . '/'. $fname) ? DIR_SERVER_ROOT . '/'. $fname : $fname;
- if (is_file($path) && is_readable($path)) {
- // Calculate the file size, etc.
- $sz = round((@filesize($path) / 1024));
-
- if ($sz < 1024) {
- $sz = $sz . "K";
- } else {
- $sz /= 1024;
-
- if ($sz < 1024) {
- $sz = round($sz, 1) . "M";
- } else {
- $sz /= 1024;
- $sz = round($sz, 2) . "G";
- }
- }
- $this->extra_info['size'] = $sz;
- $ext = substr($url, (strrpos($url, '.')));
-
- if ($ext == '.bz2' || $ext == '.gz' || $ext == '.xz' || $ext == '.7z') {
- $ext = substr($url, strrpos($url, '.', -(strlen($url) - strrpos($url, '.') + 1)));
- }
-
- if ((is_file($path . '.sha256') && is_readable($path . '.sha256'))
- && (@filemtime($path . '.sha256') > @filemtime($path))
- ) {
- $this->extra_info['sha256'] = file_get_contents($path . '.sha256');
- } else {
- $hash = hash_file('sha256', $path);
- $this->extra_info['sha256'] = $hash;
- file_put_contents($path . '.sha256', $hash);
- }
-
- $this->extra_info['ext'] = $ext;
- $this->extra_info['date'] = date('F j, Y, g:i a', @filemtime($path));
+ if (FileUtils::exists($fname)) {
+ $this->extra_info = array();
+ $this->extra_info['size'] = FileUtils::getFileSize($fname);
+ $this->extra_info['sha256'] = FileUtils::getSha256($fname);
+ $this->extra_info['ext'] = FileUtils::getExtension($fname);
+ $this->extra_info['date'] = FileUtils::getLastModified($fname);
$this->extra_info['msg'] = isset($data['notes']) ? $data['notes'] : '';
}
$this->url = $fname;
More information about the Scummvm-git-logs
mailing list