Overview
Comment:Do not delete installed files when converting
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA3-256: 8dec924397b7e95ef721070e461bd6dd13cf3be3e29d8d433cf4ee47b145e987
User & Date: bohwaz on 2022-07-28 16:10:17
Other Links: manifest | tags
Context
2022-07-28
16:41
Force graph update check-in: 61b70ea0de user: bohwaz tags: trunk, stable
16:10
Do not delete installed files when converting check-in: 8dec924397 user: bohwaz tags: trunk, stable
01:32
Implement accounting in API check-in: bda54dc686 user: bohwaz tags: trunk, stable
Changes

Modified src/include/lib/Garradin/CSV.php from [fc7dca36ae] to [4ad9e961a8].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

namespace Garradin;

use KD2\Office\Calc\Writer as ODSWriter;

class CSV
{
	/**
	 * Convert a file to CSV if required (and if CALC_CONVERT_COMMAND is set)
	 */
	static public function convertUploadIfRequired(string $path): string
	{
		if (!CALC_CONVERT_COMMAND) {
			return $path;
		}

		$mime = @mime_content_type($path);












|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

namespace Garradin;

use KD2\Office\Calc\Writer as ODSWriter;

class CSV
{
	/**
	 * Convert a file to CSV if required (and if CALC_CONVERT_COMMAND is set)
	 */
	static public function convertUploadIfRequired(string $path, bool $delete_original = false): string
	{
		if (!CALC_CONVERT_COMMAND) {
			return $path;
		}

		$mime = @mime_content_type($path);

46
47
48
49
50
51
52

53

54
55
56
57
58
59
60
			}

			self::convertXLSX($a, $b);

			return $b;
		}
		finally {

			@unlink($a);

		}
	}

	static public function convertXLSX(string $from, string $to): string
	{
		$tool = substr(CALC_CONVERT_COMMAND, 0, strpos(CALC_CONVERT_COMMAND, ' ') ?: strlen(CALC_CONVERT_COMMAND));








>
|
>







46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
			}

			self::convertXLSX($a, $b);

			return $b;
		}
		finally {
			if ($delete_original) {
				@unlink($a);
			}
		}
	}

	static public function convertXLSX(string $from, string $to): string
	{
		$tool = substr(CALC_CONVERT_COMMAND, 0, strpos(CALC_CONVERT_COMMAND, ' ') ?: strlen(CALC_CONVERT_COMMAND));

339
340
341
342
343
344
345

346

347
348
349
350
351
352
353
		}

		return self::import($file['tmp_name'], $expected_columns);
	}

	static public function import(string $file, array $expected_columns): \Generator
	{

		$file = self::convertUploadIfRequired($file);

		try {
			$fp = fopen($file, 'r');

			if (!$fp) {
				throw new UserException('Le fichier ne peut être ouvert');
			}








>
|
>







341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
		}

		return self::import($file['tmp_name'], $expected_columns);
	}

	static public function import(string $file, array $expected_columns): \Generator
	{
		$delete_after = is_uploaded_file($file);
		$file = self::convertUploadIfRequired($file, $delete_after);

		try {
			$fp = fopen($file, 'r');

			if (!$fp) {
				throw new UserException('Le fichier ne peut être ouvert');
			}

391
392
393
394
395
396
397

398

399
400
401

				yield $line => $row;
			}

			fclose($fp);
		}
		finally {

			@unlink($file);

		}
	}
}







>
|
>



395
396
397
398
399
400
401
402
403
404
405
406
407

				yield $line => $row;
			}

			fclose($fp);
		}
		finally {
			if ($delete_after) {
				@unlink($file);
			}
		}
	}
}

Modified src/include/lib/Garradin/CSV_Custom.php from [10ee1154f4] to [a93e7dc6e7].

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


48
49
50
51
52
53
54
		$this->session = $session;
		$this->key = $key;
		$this->csv = $this->session->get($this->key);
		$this->translation = $this->session->get($this->key . '_translation') ?: [];
		$this->skip = $this->session->get($this->key . '_skip') ?? 1;
	}

	public function load(array $file)
	{
		if (empty($file['size']) || empty($file['tmp_name']) || empty($file['name'])) {
			throw new UserException('Fichier invalide');
		}

		$path = $file['tmp_name'];

		if (CALC_CONVERT_COMMAND && strtolower(substr($file['name'], -4)) != '.csv') {
			$path = CSV::convertUploadIfRequired($path);
		}

		$csv = CSV::readAsArray($path);

		if (!count($csv)) {
			throw new UserException('Ce fichier est vide (aucune ligne trouvée).');
		}

		$this->session->set($this->key, $csv);


	}

	public function iterate(): \Generator
	{
		if (empty($this->csv)) {
			throw new \LogicException('No file has been loaded');
		}







|








|









>
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
		$this->session = $session;
		$this->key = $key;
		$this->csv = $this->session->get($this->key);
		$this->translation = $this->session->get($this->key . '_translation') ?: [];
		$this->skip = $this->session->get($this->key . '_skip') ?? 1;
	}

	public function load(array $file): void
	{
		if (empty($file['size']) || empty($file['tmp_name']) || empty($file['name'])) {
			throw new UserException('Fichier invalide');
		}

		$path = $file['tmp_name'];

		if (CALC_CONVERT_COMMAND && strtolower(substr($file['name'], -4)) != '.csv') {
			$path = CSV::convertUploadIfRequired($path, true);
		}

		$csv = CSV::readAsArray($path);

		if (!count($csv)) {
			throw new UserException('Ce fichier est vide (aucune ligne trouvée).');
		}

		$this->session->set($this->key, $csv);

		@unlink($path);
	}

	public function iterate(): \Generator
	{
		if (empty($this->csv)) {
			throw new \LogicException('No file has been loaded');
		}