This commit is contained in:
2024-04-19 10:27:36 +02:00
parent fcb6bbe566
commit 35c96e715c
7852 changed files with 4815 additions and 8 deletions

View File

@ -0,0 +1,80 @@
<?php
namespace App\Http\Traits;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
trait ConvertsBase64ToFiles
{
protected function base64FileKeys(): array
{
return [];
}
/**
* Pulls the Base64 contents for each file key and creates
* an UploadedFile instance from it and sets it on the
* request.
*
* @return void
*/
protected function prepareForValidation()
{
$flattened = Arr::dot($this->base64FileKeys());
Collection::make($flattened)->each(function ($filename, $key) {
rescue(function () use ($key, $filename) {
// dont process plain files
if ( $this->file($key)){
return;
}
$base64Contents = $this->input($key);
if (!$base64Contents) {
return;
}
// autogenerate filenames
if ($filename == 'auto'){
$header = explode(';', $base64Contents, 2)[0];
// Grab the image type from the header while we're at it.
$filename = $key . '.' . substr($header, strpos($header, '/')+1);
}
// Generate a temporary path to store the Base64 contents
$tempFilePath = tempnam(sys_get_temp_dir(), $filename);
// Store the contents using a stream, or by decoding manually
if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) {
$source = fopen($base64Contents, 'r');
$destination = fopen($tempFilePath, 'w');
stream_copy_to_stream($source, $destination);
fclose($source);
fclose($destination);
} else {
file_put_contents($tempFilePath, base64_decode($base64Contents, true));
}
$uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true);
\Log::debug("Trait: uploadedfile ". $tempFilePath);
$this->offsetUnset($key);
\Log::debug("Trait: encoded field \"$key\" removed" );
//Inserting new file to $this-files does not work so have to deal this after
$this->offsetSet($key,$uploadedFile);
\Log::debug("Trait: field \"$key\" inserted as UplodedFile" );
}, null, false);
});
}
}
/**
* Loosely based on idea https://github.com/protonemedia/laravel-mixins/tree/master/src/Request
* */

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Traits;
use App\Models\Asset;
trait MigratesLegacyAssetLocations
{
/**
* This is just meant to correct legacy issues where some user data would have 0
* as a location ID, which isn't valid. Later versions of Snipe-IT have stricter validation
* rules, so it's necessary to fix this for long-time users. It's kinda gross, but will help
* people (and their data) in the long run
* @param Asset $asset
* @return void
*/
private function migrateLegacyLocations(Asset $asset): void
{
if ($asset->rtd_location_id == '0') {
\Log::debug('Manually override the RTD location IDs');
\Log::debug('Original RTD Location ID: ' . $asset->rtd_location_id);
$asset->rtd_location_id = '';
\Log::debug('New RTD Location ID: ' . $asset->rtd_location_id);
}
if ($asset->location_id == '0') {
\Log::debug('Manually override the location IDs');
\Log::debug('Original Location ID: ' . $asset->location_id);
$asset->location_id = '';
\Log::debug('New Location ID: ' . $asset->location_id);
}
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Http\Traits;
trait TwoColumnUniqueUndeletedTrait
{
/**
* Prepare a unique_ids rule, adding a model identifier if required.
*
* @param array $parameters
* @param string $field
* @return string
*/
protected function prepareTwoColumnUniqueUndeletedRule($parameters, $field)
{
$column = $parameters[0];
$value = $this->{$parameters[0]};
if ($this->exists) {
return 'two_column_unique_undeleted:'.$this->table.','.$this->getKey().','.$column.','.$value;
}
return 'two_column_unique_undeleted:'.$this->table.',0,'.$column.','.$value;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Http\Traits;
trait UniqueUndeletedTrait
{
/**
* Prepare a unique_ids rule, adding a model identifier if required.
*
* @param array $parameters
* @param string $field
* @return string
*/
protected function prepareUniqueUndeletedRule($parameters, $field)
{
// Only perform a replacement if the model has been persisted.
if ($this->exists) {
return 'unique_undeleted:'.$this->table.','.$this->getKey();
}
return 'unique_undeleted:'.$this->table.',0';
}
}