Merge pull request #86 from int2001/otf_adif

OTF Zipping at ADIF-Upload
This commit is contained in:
Joerg
2024-01-12 17:13:00 +01:00
committed by GitHub
6 changed files with 115 additions and 29 deletions

View File

@@ -158,10 +158,9 @@ class adif extends CI_Controller {
public function import() {
$this->load->model('stations');
$data['station_profile'] = $this->stations->all_of_user();
log_message("debug","Started ADIF Import");
$active_station_id = $this->stations->find_active();
$station_profile = $this->stations->profile($active_station_id);
$active_station_id = $this->stations->find_active();
$station_profile = $this->stations->profile($active_station_id);
$data['active_station_info'] = $station_profile->row();
@@ -169,13 +168,14 @@ class adif extends CI_Controller {
$data['tab'] = "adif";
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'adi|ADI|adif|ADIF';
$config['allowed_types'] = 'adi|ADI|adif|ADIF|zip';
log_message("Error","ADIF Start");
session_write_close();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['max_upload'] = ini_get('upload_max_filesize');
$this->load->view('interface_assets/header', $data);
@@ -183,43 +183,71 @@ class adif extends CI_Controller {
$this->load->view('interface_assets/footer');
} else {
if ($this->stations->check_station_is_accessible($this->input->post('station_profile'))) {
$data = array('upload_data' => $this->upload->data());
$stopnow=false;
$fdata = array('upload_data' => $this->upload->data());
ini_set('memory_limit', '-1');
set_time_limit(0);
$this->load->model('logbook_model');
$this->load->library('adif_parser');
$f_elements=explode(".",$fdata['upload_data']['file_name']);
if (strtolower($f_elements[count($f_elements)-1])=='zip') {
$f_adif = preg_replace('/\\.zip$/', '', $fdata['upload_data']['file_name']);
$p_adif = preg_replace('/^(.*)(_)(\S{2,4})$/', '$1.$3', $f_adif); // Bug in CodeIgniter. Destroys Filename if there is more than one dot.
if (preg_match("/.*\.adi.?$/",strtolower($p_adif))) { // Check if adi? inside zip
$zip = new ZipArchive;
if ($zip->open('./uploads/'.$fdata['upload_data']['file_name'])) {
$zip->extractTo("./uploads/",array($p_adif));
$zip->close();
}
unlink('./uploads/'.$fdata['upload_data']['file_name']);
} else {
unlink('./uploads/'.$fdata['upload_data']['file_name']);
$data['error'] = "Unsupported Filetype";
$stopnow=true;
}
} else {
$p_adif=$fdata['upload_data']['file_name'];
}
if (!($stopnow)) {
$this->adif_parser->load_from_file('./uploads/'.$data['upload_data']['file_name']);
unlink('./uploads/'.$data['upload_data']['file_name']);
$data['upload_data']=''; // free memory
$this->load->library('adif_parser');
$this->adif_parser->initialize();
$custom_errors = "";
$alladif=[];
while($record = $this->adif_parser->get_record())
{
if(count($record) == 0) {
break;
$this->adif_parser->load_from_file('./uploads/'.$p_adif);
unlink('./uploads/'.$p_adif);
$fdata['upload_data']=''; // free memory
$this->adif_parser->initialize();
$custom_errors = "";
$alladif=[];
while($record = $this->adif_parser->get_record())
{
if(count($record) == 0) {
break;
};
array_push($alladif,$record);
};
array_push($alladif,$record);
};
$record=''; // free memory
$custom_errors = $this->logbook_model->import_bulk($alladif, $this->input->post('station_profile'), $this->input->post('skipDuplicate'), $this->input->post('markClublog'),$this->input->post('markLotw'), $this->input->post('dxccAdif'), $this->input->post('markQrz'), $this->input->post('markHrd'), true, $this->input->post('operatorName'), false, $this->input->post('skipStationCheck'));
$record=''; // free memory
$custom_errors = $this->logbook_model->import_bulk($alladif, $this->input->post('station_profile'), $this->input->post('skipDuplicate'), $this->input->post('markClublog'),$this->input->post('markLotw'), $this->input->post('dxccAdif'), $this->input->post('markQrz'), $this->input->post('markHrd'), true, $this->input->post('operatorName'), false, $this->input->post('skipStationCheck'));
} else { // Failure, if no ADIF inside ZIP
$data['max_upload'] = ini_get('upload_max_filesize');
$this->load->view('interface_assets/header', $data);
$this->load->view('adif/import', $data);
$this->load->view('interface_assets/footer');
return;
}
} else {
$custom_errors='Station Profile not valid for User';
}
log_message("Error","ADIF End");
$data['adif_errors'] = $custom_errors;
$data['skip_dupes'] = $this->input->post('skipDuplicate');
log_message("debug","Finished ADIF Import");
$data['page_title'] = "ADIF Imported";
$this->load->view('interface_assets/header', $data);
$this->load->view('adif/import_success');
$this->load->view('interface_assets/footer');
}
}

View File

@@ -3238,7 +3238,7 @@ function lotw_last_qsl_date($user_id) {
if ($dxccAdif != NULL) {
if (isset($record['dxcc'])) {
$entity = $this->get_entity($record['dxcc']);
$dxcc = array($record['dxcc'], $entity['name']);
$dxcc = array($record['dxcc'] ?? '', $entity['name'] ?? '');
} else {
$dxcc = $this->check_dxcc_table($record['call'], $time_off);
}

View File

@@ -55,7 +55,7 @@
<p><span class="badge text-bg-warning"><?php echo lang('general_word_important') ?></span> <?php echo lang('adif_alert_log_files_type') ?></p>
<p><span class="badge text-bg-warning"><?php echo lang('general_word_warning') ?></span> <?php echo lang('gen_max_file_upload_size') ?><?php echo $max_upload; ?>B.</p>
<form class="form" action="<?php echo site_url('adif/import'); ?>" method="post" enctype="multipart/form-data">
<form class="form" id="upform" action="<?php echo site_url('adif/import'); ?>" method="post" enctype="multipart/form-data">
<select name="station_profile" class="form-select mb-2 me-sm-2" style="width: 20%;">
<option value="0"><?php echo lang('adif_select_stationlocation') ?></option>
<?php foreach ($station_profile->result() as $station) { ?>
@@ -65,7 +65,7 @@
<?php } ?>
</select>
<label class="visually-hidden" for="inlineFormInputName2"><?php echo lang('adif_file_label') ?></label>
<input class="file-input mb-2 me-sm-2" type="file" name="userfile" size="20" />
<input class="file-input mb-2 me-sm-2" type="file" name="userfile" id="userfile" size="20" />
<div class="mb-3 row">
<div class="col-md-10">
@@ -145,7 +145,7 @@
</div>
</div>
<button type="submit" class="btn btn-sm btn-primary mb-2" value="Upload"><?php echo lang('adif_upload') ?></button>
<button id="prepare_sub" class="btn btn-sm btn-primary mb-2" value="Upload"><?php echo lang('adif_upload') ?></button>
</form>
</div>
@@ -265,4 +265,4 @@
</div>
</div>
</div>
</div>
</div>

View File

@@ -139,6 +139,7 @@ if($this->session->userdata('user_id') != null) {
<?php if ($this->uri->segment(1) == "adif" ) { ?>
<script src="<?php echo base_url() ;?>assets/js/sections/adif.js"></script>
<script src="<?php echo base_url() ;?>assets/js/jszip.min.js"></script>
<?php } ?>
<?php if ($this->uri->segment(1) == "notes" && ($this->uri->segment(2) == "add" || $this->uri->segment(2) == "edit") ) { ?>

13
assets/js/jszip.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,43 @@
$(document).ready(function(){
$('#prepare_sub').click(function(e){
e.preventDefault();
var fi = document.getElementById("userfile");
var file = fi.files[0];;
if (JSZip.support.blob) { // Check if Browser supports ZIP
var zip = new JSZip();
//add all files to zip
addFileToZip(file);
function addFileToZip(n) {
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onloadend = function() {
arrayBuffer = this.result;
let chker = partof(arrayBuffer,4096);
if (chker.includes('<QSO_DATE')) {
zip.file(file.name, arrayBuffer, { binary:true });
zip.generateAsync({type:"blob", compression:"DEFLATE"}).then(function(content){
//generated zip content to file type
var files = new File([content], file.name + ".zip");
const dataTransfer = new DataTransfer();
dataTransfer.items.add(files);
//send generated file to server
fi.files=dataTransfer.files;
$("#upform").submit();
return;
});
} else {
alert("Unsupported File. Must be ADIF");
}
};
fileReader.readAsArrayBuffer(file);
}
} else {
$("#upform").submit();
}
});
$('#markExportedToLotw').click(function(e){
let form = $(this).closest('form');
let station = form.find('select[name=station_profile]');
@@ -8,4 +47,9 @@ $(document).ready(function(){
form.submit();
}
})
});
function partof(buf,size) {
return String.fromCharCode.apply(null, new Uint8Array(buf.slice(0,size-1)));
}
});