Browse Source

Merge pull request #268 from viest/dev

 Feat: Check for existence before opening file
viest 5 years ago
parent
commit
5be538c30f
3 changed files with 38 additions and 0 deletions
  1. 1 0
      kernel/excel.c
  2. 13 0
      kernel/read.c
  3. 24 0
      tests/open_xlsx_file_not_found.phpt

+ 1 - 0
kernel/excel.c

@@ -295,6 +295,7 @@ PHP_METHOD(vtiful_xls, fileName)
     php_stat(ZSTR_VAL(Z_STR_P(dir_path)), strlen(ZSTR_VAL(Z_STR_P(dir_path))), FS_IS_DIR, &dir_exists);
 
     if (Z_TYPE(dir_exists) == IS_FALSE) {
+        zval_ptr_dtor(&dir_exists);
         zend_throw_exception(vtiful_exception_ce, "Configure 'path' directory does not exist", 121);
     }
 

+ 13 - 0
kernel/read.c

@@ -13,6 +13,7 @@
 #include "xlswriter.h"
 #include "ext/date/php_date.h"
 #include "ext/standard/php_math.h"
+#include "ext/standard/php_filestat.h"
 
 /* {{{ */
 xlsxioreader file_open(const char *directory, const char *file_name) {
@@ -23,13 +24,25 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
     strcat(path, "/");
     strcat(path, file_name);
 
+    zval file_exists;
+    php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
+
+    if (Z_TYPE(file_exists) == IS_FALSE) {
+        efree(path);
+        zval_ptr_dtor(&file_exists);
+        zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
+        return NULL;
+    }
+
     if ((file = xlsxioread_open(path)) == NULL) {
         efree(path);
+        zval_ptr_dtor(&file_exists);
         zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
         return NULL;
     }
 
     efree(path);
+    zval_ptr_dtor(&file_exists);
     return file;
 }
 /* }}} */

+ 24 - 0
tests/open_xlsx_file_not_found.phpt

@@ -0,0 +1,24 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php
+require __DIR__ . '/include/skipif.inc';
+skip_disable_reader();
+?>
+--FILE--
+<?php
+try {
+    $config = ['path' => './tests'];
+    $excel  = new \Vtiful\Kernel\Excel($config);
+
+    $excel->openFile('tutorial_not_found.xlsx');
+} catch (Vtiful\Kernel\Exception $exception) {
+    var_dump($exception->getMessage());
+}
+?>
+--CLEAN--
+<?php
+//
+?>
+--EXPECT--
+string(64) "File not found, please check the path in the config or file name"