2017-01-25 21:29:23 -08:00
< ? php
namespace App\Http\Controllers\Api ;
use App\Helpers\Helper ;
use App\Http\Controllers\Controller ;
use App\Http\Requests\ItemImportRequest ;
use App\Http\Transformers\ImportsTransformer ;
2019-03-13 20:12:03 -07:00
use App\Models\Asset ;
2017-01-25 21:29:23 -08:00
use App\Models\Company ;
use App\Models\Import ;
2025-01-20 05:49:42 -08:00
use Illuminate\Http\UploadedFile ;
2024-05-29 04:40:05 -07:00
use Illuminate\Support\Facades\Artisan ;
2023-02-08 12:21:51 -08:00
use Illuminate\Database\Eloquent\JsonEncodingException ;
2020-01-17 16:12:51 -08:00
use Illuminate\Support\Facades\Request ;
2017-01-25 21:29:23 -08:00
use Illuminate\Support\Facades\Session ;
2019-03-13 20:12:03 -07:00
use Illuminate\Support\Facades\Storage ;
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
use League\Csv\Reader ;
2025-01-20 05:49:42 -08:00
use Onnov\DetectEncoding\EncodingDetector ;
2017-01-25 21:29:23 -08:00
use Symfony\Component\HttpFoundation\File\Exception\FileException ;
2024-05-29 04:38:15 -07:00
use Illuminate\Support\Facades\Log ;
2024-07-04 23:07:20 -07:00
use Illuminate\Http\JsonResponse ;
2017-01-25 21:29:23 -08:00
class ImportController extends Controller
{
/**
* Display a listing of the resource .
*
*/
2024-07-04 23:07:20 -07:00
public function index () : JsonResponse | array
2017-01-25 21:29:23 -08:00
{
2019-03-18 11:58:08 -07:00
$this -> authorize ( 'import' );
2024-11-13 10:42:27 -08:00
$imports = Import :: with ( 'adminuser' ) -> latest () -> get ();
2021-06-10 13:15:52 -07:00
return ( new ImportsTransformer ) -> transformImports ( $imports );
2017-01-25 21:29:23 -08:00
}
/**
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
* Process and store a CSV upload file .
2017-01-25 21:29:23 -08:00
*
* @ param \Illuminate\Http\Request $request
*/
2024-07-04 23:07:20 -07:00
public function store () : JsonResponse
2017-01-25 21:29:23 -08:00
{
2019-03-18 11:58:08 -07:00
$this -> authorize ( 'import' );
2021-06-10 13:15:52 -07:00
if ( ! config ( 'app.lock_passwords' )) {
2020-01-17 16:12:51 -08:00
$files = Request :: file ( 'files' );
2017-01-25 21:29:23 -08:00
$path = config ( 'app.private_uploads' ) . '/imports' ;
$results = [];
$import = new Import ;
2025-01-20 05:49:42 -08:00
$detector = new EncodingDetector ();
\Log :: error ( " Okay, do we have files? " . count ( $files ));
2017-01-25 21:29:23 -08:00
foreach ( $files as $file ) {
2021-06-10 13:15:52 -07:00
if ( ! in_array ( $file -> getMimeType (), [
2017-01-25 21:29:23 -08:00
'application/vnd.ms-excel' ,
'text/csv' ,
2021-04-23 12:09:00 -07:00
'application/csv' ,
'text/x-Algol68' , // because wtf CSV files?
2017-01-25 21:29:23 -08:00
'text/plain' ,
'text/comma-separated-values' ,
2021-06-10 13:15:52 -07:00
'text/tsv' , ])) {
$results [ 'error' ] = 'File type must be CSV. Uploaded file is ' . $file -> getMimeType ();
2025-01-20 05:49:42 -08:00
\Log :: error ( " Bad mime type " );
2023-02-08 12:39:42 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $results [ 'error' ]), 422 );
2017-01-25 21:29:23 -08:00
}
2018-03-22 19:16:15 -07:00
//TODO: is there a lighter way to do this?
2021-06-10 13:15:52 -07:00
if ( ! ini_get ( 'auto_detect_line_endings' )) {
ini_set ( 'auto_detect_line_endings' , '1' );
2018-03-22 19:16:15 -07:00
}
2025-01-20 05:49:42 -08:00
$file_contents = $file -> getContent (); //TODO - this *does* load the whole file in RAM, but we need that to be able to 'iconv' it?
$encoding = $detector -> getEncoding ( $file_contents );
$reader = null ;
if ( strcasecmp ( $encoding , 'UTF-8' ) != 0 ) {
\Log :: error ( " Weird encoding detected: $encoding " );
$transliterated = iconv ( $encoding , 'UTF-8' , $file_contents );
if ( $transliterated !== false ) {
\Log :: error ( " Transliteration was successful. " );
$tmpname = tempnam ( sys_get_temp_dir (), '' );
$tmpresults = file_put_contents ( $tmpname , $transliterated );
if ( $tmpresults !== false ) {
\Log :: error ( " Temporary file written to $tmpname " );
$transliterated = null ; //save on memory?
$newfile = new UploadedFile ( $tmpname , $file -> getClientOriginalName (), null , null , true ); //WARNING: this is enabling 'test mode' - which is gross, but otherwise the file won't be treated as 'uploaded'
if ( $newfile -> isValid ()) {
\Log :: error ( " new UploadedFile was created " );
$file = $newfile ;
}
}
}
}
2018-03-22 19:16:15 -07:00
$reader = Reader :: createFromFileObject ( $file -> openFile ( 'r' )); //file pointer leak?
2025-01-20 05:49:42 -08:00
$file_contents = null ; //try to save on memory, I guess?
2023-02-08 12:21:51 -08:00
try {
$import -> header_row = $reader -> fetchOne ( 0 );
} catch ( JsonEncodingException $e ) {
2025-01-20 05:49:42 -08:00
\Log :: error ( " cant load header row? " );
2023-02-08 12:21:51 -08:00
return response () -> json (
Helper :: formatStandardApiResponse (
'error' ,
null ,
2023-02-13 13:30:36 -08:00
trans ( 'admin/hardware/message.import.header_row_has_malformed_characters' )
2023-02-08 12:32:57 -08:00
),
2023-02-08 12:39:42 -08:00
422
2023-02-08 12:21:51 -08:00
);
}
2018-03-22 19:16:15 -07:00
//duplicate headers check
$duplicate_headers = [];
2021-06-10 13:15:52 -07:00
for ( $i = 0 ; $i < count ( $import -> header_row ); $i ++ ) {
2018-03-22 19:16:15 -07:00
$header = $import -> header_row [ $i ];
2021-06-10 13:15:52 -07:00
if ( in_array ( $header , $import -> header_row )) {
2018-03-22 19:16:15 -07:00
$found_at = array_search ( $header , $import -> header_row );
2021-06-10 13:15:52 -07:00
if ( $i > $found_at ) {
2018-03-22 19:16:15 -07:00
//avoid reporting duplicates twice, e.g. "1 is same as 17! 17 is same as 1!!!"
//as well as "1 is same as 1!!!" (which is always true)
//has to be > because otherwise the first result of array_search will always be $i itself(!)
2021-06-10 13:15:52 -07:00
array_push ( $duplicate_headers , " Duplicate header ' $header ' detected, first at column: " . ( $found_at + 1 ) . ', repeats at column: ' . ( $i + 1 ));
2018-03-22 19:16:15 -07:00
}
}
}
2021-06-10 13:15:52 -07:00
if ( count ( $duplicate_headers ) > 0 ) {
2025-01-20 05:49:42 -08:00
\Log :: error ( " Duplicate headers? " );
2023-02-08 12:39:42 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , implode ( '; ' , $duplicate_headers )), 422 );
2018-03-22 19:16:15 -07:00
}
2023-02-08 12:21:51 -08:00
try {
// Grab the first row to display via ajax as the user picks fields
$import -> first_row = $reader -> fetchOne ( 1 );
} catch ( JsonEncodingException $e ) {
2025-01-20 05:49:42 -08:00
\Log :: error ( " JSON eocding reception? " );
2023-02-08 12:21:51 -08:00
return response () -> json (
Helper :: formatStandardApiResponse (
'error' ,
null ,
2023-02-13 13:30:36 -08:00
trans ( 'admin/hardware/message.import.content_row_has_malformed_characters' )
2023-02-08 12:32:57 -08:00
),
2023-02-08 12:39:42 -08:00
422
2023-02-08 12:21:51 -08:00
);
}
2018-03-22 19:16:15 -07:00
2017-01-25 21:29:23 -08:00
$date = date ( 'Y-m-d-his' );
2017-07-14 02:38:13 -07:00
$fixed_filename = str_slug ( $file -> getClientOriginalName ());
2017-01-25 21:29:23 -08:00
try {
$file -> move ( $path , $date . '-' . $fixed_filename );
} catch ( FileException $exception ) {
2021-06-10 13:15:52 -07:00
$results [ 'error' ] = trans ( 'admin/hardware/message.upload.error' );
2017-01-25 21:29:23 -08:00
if ( config ( 'app.debug' )) {
2021-06-10 13:15:52 -07:00
$results [ 'error' ] .= ' ' . $exception -> getMessage ();
2017-01-25 21:29:23 -08:00
}
2021-06-10 13:15:52 -07:00
2017-01-25 21:29:23 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $results [ 'error' ]), 500 );
}
$file_name = date ( 'Y-m-d-his' ) . '-' . $fixed_filename ;
$import -> file_path = $file_name ;
2023-03-06 10:47:28 -08:00
$import -> filesize = null ;
2023-03-06 15:09:37 -08:00
if ( ! file_exists ( $path . '/' . $file_name )) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'general.file_not_found' )), 500 );
2023-03-06 10:47:28 -08:00
}
2023-03-06 15:09:37 -08:00
$import -> filesize = filesize ( $path . '/' . $file_name );
2024-11-13 10:42:27 -08:00
$import -> created_by = auth () -> id ();
2017-01-25 21:29:23 -08:00
$import -> save ();
$results [] = $import ;
}
$results = ( new ImportsTransformer ) -> transformImports ( $results );
2021-06-10 13:15:52 -07:00
2023-02-08 12:34:25 -08:00
return response () -> json ([
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
'files' => $results ,
2023-02-08 12:34:25 -08:00
]);
2017-01-25 21:29:23 -08:00
}
2021-06-10 13:15:52 -07:00
2023-02-08 12:39:42 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'general.feature_disabled' )), 422 );
2017-01-25 21:29:23 -08:00
}
2021-06-10 13:15:52 -07:00
2017-01-25 21:29:23 -08:00
/**
* Processes the specified Import .
*
2018-08-01 18:01:16 -07:00
* @ param int $import_id
2017-01-25 21:29:23 -08:00
*/
2024-07-04 23:07:20 -07:00
public function process ( ItemImportRequest $request , $import_id ) : JsonResponse
2017-01-25 21:29:23 -08:00
{
2019-03-18 11:58:08 -07:00
$this -> authorize ( 'import' );
2020-05-11 22:57:55 -07:00
2017-09-25 15:00:23 -07:00
// Run a backup immediately before processing
2023-01-23 15:08:59 -08:00
if ( $request -> get ( 'run-backup' )) {
2024-05-29 04:38:15 -07:00
Log :: debug ( 'Backup manually requested via importer' );
2023-05-08 14:48:26 -07:00
Artisan :: call ( 'snipeit:backup' , [ '--filename' => 'pre-import-backup-' . date ( 'Y-m-d-H:i:s' )]);
2020-05-11 22:57:55 -07:00
} else {
2024-05-29 04:38:15 -07:00
Log :: debug ( 'NO BACKUP requested via importer' );
2020-05-11 22:57:55 -07:00
}
2022-05-18 04:15:46 -07:00
$import = Import :: find ( $import_id );
if ( is_null ( $import )){
$error [ 0 ][ 0 ] = trans ( " validation.exists " , [ " attribute " => " file " ]);
return response () -> json ( Helper :: formatStandardApiResponse ( 'import-errors' , null , $error ), 500 );
}
$errors = $request -> import ( $import );
2021-06-10 13:15:52 -07:00
$redirectTo = 'hardware.index' ;
2017-01-25 21:29:23 -08:00
switch ( $request -> get ( 'import-type' )) {
2021-06-10 13:15:52 -07:00
case 'asset' :
$redirectTo = 'hardware.index' ;
2017-01-25 21:29:23 -08:00
break ;
2024-11-13 10:42:27 -08:00
case 'assetModel' :
2024-11-12 14:40:00 -08:00
$redirectTo = 'models.index' ;
break ;
2021-06-10 13:15:52 -07:00
case 'accessory' :
$redirectTo = 'accessories.index' ;
2017-01-25 21:29:23 -08:00
break ;
2021-06-10 13:15:52 -07:00
case 'consumable' :
$redirectTo = 'consumables.index' ;
2017-01-25 21:29:23 -08:00
break ;
2021-06-10 13:15:52 -07:00
case 'component' :
$redirectTo = 'components.index' ;
2017-01-25 21:29:23 -08:00
break ;
2021-06-10 13:15:52 -07:00
case 'license' :
$redirectTo = 'licenses.index' ;
2017-01-25 21:29:23 -08:00
break ;
2021-06-10 13:15:52 -07:00
case 'user' :
$redirectTo = 'users.index' ;
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
break ;
2023-04-16 07:47:49 -07:00
case 'location' :
$redirectTo = 'locations.index' ;
break ;
2017-01-25 21:29:23 -08:00
}
if ( $errors ) { //Failure
return response () -> json ( Helper :: formatStandardApiResponse ( 'import-errors' , null , $errors ), 500 );
}
//Flash message before the redirect
Session :: flash ( 'success' , trans ( 'admin/hardware/message.import.success' ));
2021-06-10 13:15:52 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , null , [ 'redirect_url' => route ( $redirectTo )]));
2017-01-25 21:29:23 -08:00
}
/**
* Remove the specified resource from storage .
*
2018-08-01 18:01:16 -07:00
* @ param int $import_id
2017-01-25 21:29:23 -08:00
*/
2024-07-04 23:07:20 -07:00
public function destroy ( $import_id ) : JsonResponse
2017-01-25 21:29:23 -08:00
{
$this -> authorize ( 'create' , Asset :: class );
2021-06-10 13:15:52 -07:00
2018-08-01 20:49:55 -07:00
if ( $import = Import :: find ( $import_id )) {
try {
// Try to delete the file
2018-09-29 21:33:52 -07:00
Storage :: delete ( 'imports/' . $import -> file_path );
2018-08-01 20:49:55 -07:00
$import -> delete ();
2021-06-10 13:15:52 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , null , trans ( 'admin/hardware/message.import.file_delete_success' )));
2018-08-01 20:49:55 -07:00
} catch ( \Exception $e ) {
// If the file delete didn't work, remove it from the database anyway and return a warning
$import -> delete ();
2021-06-10 13:15:52 -07:00
2018-08-02 09:53:54 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'warning' , null , trans ( 'admin/hardware/message.import.file_not_deleted_warning' )));
2018-08-01 20:49:55 -07:00
}
2024-07-04 23:07:20 -07:00
2017-01-25 21:29:23 -08:00
}
2024-07-04 23:07:20 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'warning' , null , trans ( 'admin/hardware/message.import.file_not_deleted_warning' )));
2017-01-25 21:29:23 -08:00
}
}