Skip to content

Commit

Permalink
feat: better file locating for mvl exports (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobybatch authored Apr 30, 2024
1 parent 06c164a commit b88d681
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
66 changes: 39 additions & 27 deletions app/Http/Controllers/Store/VoucherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,56 +101,45 @@ public function listVoucherLogs()
{
// by gabriel (intern 4)

$directoryPath = storage_path("app/local"); # I think I'm using the wrong function to access files?
$directoryPath = Storage::path('enc');

$logFiles = File::glob($directoryPath . '/*.arcx.csv');

$downloadLinks = [];
$logMetadata = [];
rsort($logFiles);
$logFilesMetadata = [];
foreach ($logFiles as $logFile) {
$baseFileName = basename($logFile);

$downloadLinks[$baseFileName] = "/vouchers/download?logFile=" . $baseFileName;

$rawFileSize = filesize($logFile);

$formattedFileSize = TextFormatter::formatBytes($rawFileSize);
$logFilesMetadata[$logFile] = "xxx";
}

$logMetadata[$baseFileName] = [
"fileName" => $baseFileName,
"rawSize" => filesize($logFile), // Use for sorting.
"formattedSize" => $formattedFileSize, // Use for displaying.
"lastModified" => filemtime($logFile)];
$data = [];
foreach ($logFilesMetadata as $fileName => $metadata) {
$data[$this->makeDisplayName($fileName)] = [
"displayDate" => date("d/m/Y H:s", filemtime($fileName)),
"fileSize" => TextFormatter::formatBytes(filesize($fileName)),
"downloadLink" => "/vouchers/download?logFile=" . $fileName,
];
}

return view('store.list_voucher_logs', ["downloadLinks" => $downloadLinks, "logMetadata" => $logMetadata]);
return view('store.list_voucher_logs', ["data" => $data]);
}

public function downloadVoucherLogs(Request $request)
{
$logFile = $request->query('logFile');
return response()->download(storage_path("app/local/" . $logFile));
}


public function downloadAndDecryptVoucherLogs(Request $request)
{
$logFile = $request->query('logFile');
// Is there a way for people to maliciously pass bad queries?
// I don't think they can do this, since it checks only searches within storage/app/local.
// The website will crash though if the file isn't encrypted properly/at all.

$pathToVouchers = "app/local/";
$pathToVouchers = Storage::path('enc') . '/' . $logFile;

// Check the log exists, so we can error-out before we declare a streamed response.
if (!file_exists(storage_path($pathToVouchers . $logFile))) {
if (!file_exists($pathToVouchers)) {
// Return to dashboard with an error that indicates we don't have a file.
return redirect(URL::route('store.dashboard'))
->with('error_message', "Sorry, we couldn't find the file you were looking for. Please contact support if this error persists.");
}

// Do as much IO as we can comfortably before we begin streaming.
$file = fopen(storage_path($pathToVouchers . $logFile), 'r');
$file = fopen($pathToVouchers, 'r');

$header = fread($file, SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES);

Expand Down Expand Up @@ -204,4 +193,27 @@ public function downloadAndDecryptVoucherLogs(Request $request)
]);

}

private function makeDisplayName($filename)
{
$basename = basename($filename);
$start = date_parse_from_format(
"Ymd",
substr($basename, 9, 8)
);
$end = date_parse_from_format(
"Ymd",
substr($basename, 21, 8)
);

return sprintf(
"%02d/%02d/%d to %02d/%02d/%d",
$start["day"],
$start["month"],
$start["year"],
$end["day"],
$end["month"],
$end["year"],
);
}
}
14 changes: 7 additions & 7 deletions resources/views/store/list_voucher_logs.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
<table id='voucherExportTable'>
<thead>
<tr>
<th class="center">File Name</th>
<th class="center">Date Included</th>
<th class="center">File Size</th>
<th class="center">Last Modified</th>
<th class="center">Download Link</th>
</tr>
</thead>
<tbody>
@foreach ($downloadLinks as $fileName => $web_link)
@foreach ($data as $displayName => $metadata)
<tr class="active">
<td class="center">{{$fileName}}</td>
<td class="center">{{$logMetadata[$fileName]["formattedSize"]}}</td>
<td class="center">{{gmdate("D, d M Y H:i:s", $logMetadata[$fileName]["lastModified"])}}</td>
<td class="center"> {{--WHY IS IT OFF-CENTRE--}}
<a href="{{$web_link}}" target="_blank" class="centre link inline-link-button">
<td class="center">{{$displayName}}</td>
<td class="center">{{$metadata["fileSize"]}}</td>
<td class="center">{{$metadata["displayDate"]}}</td>
<td class="center">
<a href="{{$metadata["downloadLink"]}}" target="_blank" class="centre link inline-link-button">
<div class="link-button">
<i class="fa fa-download" aria-hidden="true"></i> Download
</div>
Expand Down

0 comments on commit b88d681

Please sign in to comment.