fake afnor

This commit is contained in:
afornerot 2023-01-03 10:50:07 +01:00
parent e7f7a348da
commit e8f528574a
9 changed files with 152 additions and 35 deletions

13
.env
View File

@ -1,4 +1,4 @@
APP_ENV=prod
APP_ENV=dev
APP_SECRET=changeme
MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0
@ -10,9 +10,10 @@ MERCURE_URL=https://example.com/.well-known/mercure
MERCURE_PUBLIC_URL=https://example.com/.well-known/mercure
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"
SFTP_HOST=localhost
SFTP_PORT=2222
SFTP_USER=user
SFTP_PASSWORD=changeme
SFTP_FOLDER=upload
SFTP_HOST=$SFTP_HOST
SFTP_PORT=$SFTP_PORT
SFTP_USER=$SFTP_USER
SFTP_PASSWORD=$SFTP_PASSWORD
SFTP_FOLDER=$SFTP_FOLDER
FAKE_FILES=$FAKE_FILES

1
.gitignore vendored
View File

@ -7,6 +7,7 @@
/public/bundles/
/var/
/vendor/
/sftp/
###< symfony/framework-bundle ###
###> symfony/webpack-encore-bundle ###

View File

@ -30,6 +30,17 @@ $(document).ready(function() {
$("#"+$(this).data("modalid")).modal("show");
});
$('.add-another-collection-widget').click(function (e) {
var list = $($(this).attr('data-list-selector'));
var counter = list.data('widget-counter') || list.children().length;
var newWidget = list.attr('data-prototype');
newWidget = newWidget.replace(/__name__/g, counter);
counter++;
list.data('widget-counter', counter);
var newElem = $(list.attr('data-widget-tags')).html(newWidget);
newElem.appendTo(list);
});
// Resize
resize();

View File

@ -4,6 +4,7 @@ parameters:
sftpUser: '%env(resolve:SFTP_USER)%'
sftpPassword: '%env(resolve:SFTP_PASSWORD)%'
sftpFolder: '%env(resolve:SFTP_FOLDER)%'
fakeFiles: '%env(json:FAKE_FILES)%'
services:
_defaults:

View File

@ -5,31 +5,7 @@ services:
image: atmoz/sftp
container_name: sftp
volumes:
- ./sftp:/home/efs/upload
- ./sftp:/home/user/upload
ports:
- "2222:22"
command: efs:changeme:1000
###> symfony/mercure-bundle ###
mercure:
image: dunglas/mercure
restart: unless-stopped
environment:
SERVER_NAME: ':80'
MERCURE_PUBLISHER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
# Set the URL of your Symfony project (without trailing slash!) as value of the cors_origins directive
MERCURE_EXTRA_DIRECTIVES: |
cors_origins http://127.0.0.1:8000
# Comment the following line to disable the development mode
command: /usr/bin/caddy run -config /etc/caddy/Caddyfile.dev
volumes:
- mercure_data:/data
- mercure_config:/config
###< symfony/mercure-bundle ###
volumes:
###> symfony/mercure-bundle ###
mercure_data:
mercure_config:
###< symfony/mercure-bundle ###
command: user:changeme:::upload

View File

@ -4,12 +4,14 @@ namespace App\Controller;
use App\Form\FolderType;
use App\Form\FileType;
use App\Form\AfnorType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Service\SftpService;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Filesystem\Filesystem;
class HomeController extends AbstractController
{
@ -42,6 +44,31 @@ class HomeController extends AbstractController
$this->sftp->rename($folder."/".$oldname,$folder."/".$name);
}
// Fake AFNOR
$formAfnor = $this->createForm(AfnorType::class);
$formAfnor->handleRequest($request);
if ($formAfnor->get('submit')->isClicked()) {
$id = $formAfnor->get("id")->getData();
$codeproduct = $formAfnor->get("codeproduct")->getData();
$codequalifications = $formAfnor->get("codequalifications")->getData();
$filesystem = new Filesystem();
$dir = $this->getParameter('kernel.project_dir').'/var/tmp';
$file ="gc,".$id."\n";
$file.="ph\n";
$file.="pj,".$codeproduct."\n";
foreach($codequalifications as $codequalification) {
$file.="qa\n";
$file.="qh,".$codequalification."\n";
}
$filesystem->dumpFile($dir.'/DEL-'.$id.'.AFN', $file);
$return=$this->sftp->uploadFile($dir.'/DEL-'.$id.'.AFN', $folder);
}
// Lister les fichiers
$ls=$this->sftp->ls($folder);
@ -65,8 +92,11 @@ class HomeController extends AbstractController
'infolder' => $folder,
'folders' => $ls["folders"],
'files' => $ls["files"],
'fakeAfnor'=>in_array("AFNOR",$this->getParameter("fakeFiles")),
'formfolder'=>$formFolder->createView(),
'formfile'=>$formFile->createView(),
'formafnor'=>$formAfnor->createView(),
]);
}
@ -78,7 +108,7 @@ class HomeController extends AbstractController
throw new \Exception('Erreur de téléchargement');
}
if (str_starts_with(mime_content_type($tmpfile), 'image/') || 'application/pdf' == mime_content_type($tmpfile)) {
if (str_starts_with(mime_content_type($tmpfile), 'image/') || 'application/pdf' == mime_content_type($tmpfile) || 'text/plain' == mime_content_type($tmpfile)) {
$response = new BinaryFileResponse($tmpfile);
$response->headers->set('Content-Type', mime_content_type($tmpfile));
} else {

53
src/Form/AfnorType.php Normal file
View File

@ -0,0 +1,53 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AfnorType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('submit',
SubmitType::class, [
'label' => 'Valider',
'attr' => ['class' => 'btn btn-success mb-5'],
]
);
$builder->add('id',
TextType::class, [
'mapped' => false,
'label' => 'ID Déliverance',
'required' => true,
]
);
$builder->add('codeproduct',
TextType::class, [
'mapped' => false,
'label' => 'Code Produit',
'required' => true,
]
);
$builder->add('codequalifications', CollectionType::class, [
'entry_type' => TextType::class,
'label' => 'Codes Qualification',
'allow_add' => true,
'required' => false,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
]);
}
}

View File

@ -71,10 +71,8 @@ class SftpService
$sftp = $this->sftp;
$remotePath = 'ssh2.sftp://'.join(DIRECTORY_SEPARATOR, [$sftp, $this->folder.$remoteFolder."/".$baseName]);
echo($remotePath);
$stream = @fopen($remotePath, 'w');
if (!$stream) {
echo "here";
return false;
}
$localStream = fopen($localFile, 'r');

View File

@ -2,6 +2,12 @@
{% block body %}
<div class="float-end">
{% if fakeAfnor %}
<button id="addafnor" onClick="showAfnor()" type="button" class="btn btn_link" data-toggle="modal" data-target="#afnor">
AFNOR
</button>
{% endif %}
<button id="addfolder" onClick="showFolder()" type="button" class="btn btn_link" data-toggle="modal" data-target="#folder">
<i class="fa fa-folder-plus fa-2x fa-fw"></i>
</button>
@ -77,6 +83,38 @@
</div>
</div>
</div>
<div id="afnor" class="modal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Génération Fichier AFNOR</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{{ form_start(formafnor) }}
{{ form_row(formafnor.submit) }}
{{ form_row(formafnor.id) }}
{{ form_row(formafnor.codeproduct) }}
{{ form_label(formafnor.codequalifications) }}
<ul id="codequalifications-list"
data-prototype="{{ form_widget(formafnor.codequalifications.vars.prototype)|e }}"
data-widget-tags="{{ '<li></li>'|e }}"
data-widget-counter="{{ formafnor.codequalifications|length }}">
{% for codequalification in formafnor.codequalifications %}
<li>
{{ form_errors(codequalification) }}
{{ form_widget(codequalification) }}
</li>
{% endfor %}
</ul>
{{ form_end(formafnor, {render_rest: false}) }}
<button type="button" class="add-another-collection-widget btn btn-primary" data-list-selector="#codequalifications-list">Ajouter Qualification</button>
</div>
</div>
</div>
</div>
{% endblock %}
{% block localscript %}
@ -92,5 +130,13 @@
$('#file_name').val(file);
$('#file_name').focus();
}
function showAfnor() {
$('#afnor').modal('show');
}
</script>
{% endblock %}