Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,28 @@ public function check()
'403 Forbidden'
);

// Get all documents
$documents = Document::all();
// Get all documents with computed metadata
$documents = Document::all()->map(function ($doc) {
$filePath = storage_path('docs/' . $doc->id);
$fileExists = file_exists($filePath);

// Add computed attributes to document object
$doc->file_exists = $fileExists;
$doc->link_count = 0;
$doc->hash_valid = false;

if ($fileExists) {
$stats = stat($filePath);
$doc->link_count = $stats['nlink'] ?? 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Guard stat() return value before array access.

If the file is deleted between the file_exists() check and stat() call (TOCTOU race), stat() returns false. Accessing $stats['nlink'] on false will produce a PHP warning.

🛡️ Proposed fix
             if ($fileExists) {
                 $stats = stat($filePath);
-                $doc->link_count = $stats['nlink'] ?? 0;
+                if ($stats !== false) {
+                    $doc->link_count = $stats['nlink'] ?? 0;
+                }

                 $computedHash = hash_file('sha256', $filePath);
🤖 Prompt for AI Agents
In `@app/Http/Controllers/DocumentController.php` around lines 284 - 285,
stat($filePath) can return false if the file is removed between file_exists()
and stat() (TOCTOU); update the block in DocumentController (around the $stats =
stat($filePath) and $doc->link_count assignment) to check that $stats !== false
(or is_array($stats)) before accessing $stats['nlink'], and fall back to 0 when
stat() failed so $doc->link_count is always safely assigned.


$computedHash = hash_file('sha256', $filePath);
$doc->hash_valid = $computedHash !== false && hash_equals($doc->hash, $computedHash);
}
Comment thread
dbarzin marked this conversation as resolved.

return $doc;
});

// show view
// Show view with pre-computed metadata
return view('/documents/check')
->with('documents', $documents);
}
Expand Down
38 changes: 22 additions & 16 deletions resources/views/documents/check.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,43 @@
<th>{{ trans('cruds.document.fields.name') }}</th>
<th>{{ trans('cruds.document.fields.size') }}</th>
<th>{{ trans('cruds.document.fields.hash') }}</th>
<th>Links</th>
<th>Status</th>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add translation keys for new column headers.

Other column headers use trans('cruds.document.fields.xxx') for internationalization, but "Links" and "Status" are hardcoded English strings. This breaks consistency for localized deployments.

🌐 Proposed fix
-        <th>Links</th>
-        <th>Status</th>
+        <th>{{ trans('cruds.document.fields.links') }}</th>
+        <th>{{ trans('cruds.document.fields.status') }}</th>

Don't forget to add the corresponding keys to your language files.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<th>Links</th>
<th>Status</th>
<th>{{ trans('cruds.document.fields.links') }}</th>
<th>{{ trans('cruds.document.fields.status') }}</th>
🤖 Prompt for AI Agents
In `@resources/views/documents/check.blade.php` around lines 14 - 15, Replace the
hardcoded column headers "Links" and "Status" in the Blade view with translation
helpers (e.g. use trans('cruds.document.fields.links') and
trans('cruds.document.fields.status') or `@lang`(...) in the same locations), and
add the corresponding keys ("links" and "status") under cruds.document.fields in
your language files so the labels render correctly for localized deployments.

</tr>
</thead>

@foreach ($documents as $doc)
<tr>
<td>
{{ $doc->id }}
</td>
<td>{{ $doc->id }}</td>
<td class="text-center">
<a href="/bob/show/{{ $doc->control_id }}">{{ $doc->control_id }}</a>
</td>
<td>
<a href="/doc/show/{{ $doc->id }}">{{ substr($doc->filename,0,32) }}</a>
<a href="/doc/show/{{ $doc->id }}">{{ substr($doc->filename, 0, 32) }}</a>
</td>
<td>
{{ \Illuminate\Support\Number::fileSize($doc->size) }}
</td>
<td>
{{ $doc->hash }}
<br>
<td>{{ \Illuminate\Support\Number::fileSize($doc->size) }}</td>
<td><small>{{ $doc->hash }}</small></td>
<td class="text-center">
@if ($doc->file_exists)
@if ($doc->link_count > 1)
<span class="badge bg-blue">{{ $doc->link_count }}</span>
@else
{{ $doc->link_count }}
@endif
@else
-
@endif
</td>
<td>
<b>
@if (file_exists(storage_path('docs/').$doc->id))
@if ($doc->hash == hash_file("sha256", storage_path('docs/').$doc->id))
<font color="green">OK</font>
@if ($doc->file_exists)
@if ($doc->hash_valid)
<span style="color: green;">OK</span>
@else
<font color="red">HASH FAILS</font>
<span style="color: red">HASH FAILS</span>
@endif
@else
<font color="red">MISSING</font>
<span style="color: red">MISSING</span>
@endif
</b>
</td>
Expand All @@ -50,4 +56,4 @@
</table>
</div>

@endsection
@endsection