• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aimeos / aimeos-core / b09a32f6-a0c3-444a-accc-2e0c50d3c68a

14 Jun 2024 01:23PM UTC coverage: 91.669% (+0.001%) from 91.668%
b09a32f6-a0c3-444a-accc-2e0c50d3c68a

push

circleci

aimeos
Only warn if files can't be deleted

6 of 6 new or added lines in 2 files covered. (100.0%)

4 existing lines in 2 files now uncovered.

11091 of 12099 relevant lines covered (91.67%)

54.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

32.14
/src/MShop/Upload.php
1
<?php
2

3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023
6
 * @package MShop
7
 */
8

9

10
namespace Aimeos\MShop;
11

12
use \Psr\Http\Message\UploadedFileInterface;
13

14

15
/**
16
 * Upload trait
17
 *
18
 * @package MShop
19
 */
20
trait Upload
21
{
22
        /**
23
         * Returns the context object.
24
         *
25
         * @return \Aimeos\MShop\ContextIface Context object
26
         */
27
        abstract protected function context() : \Aimeos\MShop\ContextIface;
28

29

30
        /**
31
         * Deletes a file from the file system
32
         *
33
         * @param string $filepath Relative path to the file in the file system
34
         * @param string $fsname File system name
35
         * @return self Same object for fluent interface
36
         */
37
        protected function deleteFile( string $filepath, string $fsname = 'fs-media' ) : self
38
        {
39
                if( $filepath ) {
2✔
40
                        $this->context()->fs( $fsname )->rm( $filepath );
2✔
41
                }
42

UNCOV
43
                return $this;
×
44
        }
45

46

47
        /**
48
         * Returns the file mime type for the uploaded file
49
         *
50
         * Caution: This method must be called before storeFile() to be able to
51
         * determine the file mime type!
52
         *
53
         * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file object
54
         * @return string File mime type
55
         */
56
        protected function mimetype( UploadedFileInterface $file ) : string
57
        {
58
                $stream = $file->getStream();
3✔
59

60
                if( !$stream->isSeekable() ) {
2✔
61
                        return '';
×
62
                }
63

64
                $stream->rewind();
2✔
65
                $content = $stream->read( 100 );
2✔
66
                $stream->rewind();
2✔
67

68
                $finfo = new \finfo( FILEINFO_MIME_TYPE );
2✔
69
                return $finfo->buffer( $content );
2✔
70
        }
71

72

73
        /**
74
         * Stores the uploaded file
75
         *
76
         * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file object
77
         * @param string $filepath Relative path to the file in the file system
78
         * @param string $fsname File system name
79
         * @return self Same object for fluent interface
80
         */
81
        protected function storeFile( UploadedFileInterface $file, string $filepath, string $fsname = 'fs-media' ) : self
82
        {
83
                if( ( $code = $file->getError() ) !== UPLOAD_ERR_OK )
×
84
                {
85
                        $errors = [
×
86
                                0 => 'There is no error, the file uploaded with success',
×
87
                                1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
×
88
                                2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
×
89
                                3 => 'The uploaded file was only partially uploaded',
×
90
                                4 => 'No file was uploaded',
×
91
                                6 => 'Missing a temporary folder',
×
92
                                7 => 'Failed to write file to disk.',
×
93
                                8 => 'A PHP extension stopped the file upload.',
×
94
                        ];
×
95

96
                        throw new \RuntimeException( $errors[$code] ?? sprintf( 'An unknown error occured with code %1$d', $code ) );
×
97
                }
98

99
                $fs = $this->context()->fs( $fsname );
×
100

101
                if( ( $fs instanceof \Aimeos\Base\Filesystem\DirIface ) && !$fs->has( $dirname = dirname( $filepath ) ) ) {
×
102
                        $fs->mkdir( $dirname );
×
103
                }
104

105
                $fs->writes( $filepath, $file->getStream()->detach() );
×
106

107
                return $this;
×
108
        }
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc