1<?php
2
3namespace LM\Bundle\CoreBundle\Service;
4
5use Symfony\Component\HttpFoundation\File\UploadedFile;
6use Gaufrette\Filesystem;
7
8class PhotoUploader
9{
10 private static $allowedMimeTypes = array('image/jpeg', 'image/png', 'image/gif');
11 private $filesystem;
12
13 public function __construct(Filesystem $filesystem)
14 {
15 $this->filesystem = $filesystem;
16 }
17
18 public function upload(UploadedFile $file)
19 {
20 // Check if the file's mime type is in the list of allowed mime types.
21 if (!in_array($file->getClientMimeType(), self::$allowedMimeTypes)) {
22 throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
23 }
24
25 // Generate a unique filename based on the date and add file extension of the uploaded file
26 $filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), uniqid(), $file->getClientOriginalExtension());
27
28 $adapter = $this->filesystem->getAdapter();
29 $adapter->setMetadata($filename, array('contentType' => $file->getClientMimeType()));
30 $adapter->write($filename, file_get_contents($file->getPathname()));
31
32 return $filename;
33 }
34
35 public function uploadFromUrl($url)
36 {
37 // Get file extension
38 $extension = pathinfo($url, PATHINFO_EXTENSION);
39
40 // Generate a unique filename based on the date and add file extension of the uploaded file
41 $filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), uniqid(), $extension);
42
43 // Guess mime type
44 $mimeType = $this->guessMimeType($extension);
45
46 $adapter = $this->filesystem->getAdapter();
47 $adapter->setMetadata($filename, array('contentType' => $mimeType));
48 $adapter->write($filename, file_get_contents($url));
49
50 return $filename;
51 }
52
53 private function guessMimeType($extension)
54 {
55 $mimeTypes = array(
56
57 'txt' => 'text/plain',
58 'htm' => 'text/html',
59 'html' => 'text/html',
60 'php' => 'text/html',
61 'css' => 'text/css',
62 'js' => 'application/javascript',
63 'json' => 'application/json',
64 'xml' => 'application/xml',
65 'swf' => 'application/x-shockwave-flash',
66 'flv' => 'video/x-flv',
67
68 // images
69 'png' => 'image/png',
70 'jpe' => 'image/jpeg',
71 'jpeg' => 'image/jpeg',
72 'jpg' => 'image/jpeg',
73 'gif' => 'image/gif',
74 'bmp' => 'image/bmp',
75 'ico' => 'image/vnd.microsoft.icon',
76 'tiff' => 'image/tiff',
77 'tif' => 'image/tiff',
78 'svg' => 'image/svg+xml',
79 'svgz' => 'image/svg+xml',
80
81 // archives
82 'zip' => 'application/zip',
83 'rar' => 'application/x-rar-compressed',
84 'exe' => 'application/x-msdownload',
85 'msi' => 'application/x-msdownload',
86 'cab' => 'application/vnd.ms-cab-compressed',
87
88 // audio/video
89 'mp3' => 'audio/mpeg',
90 'qt' => 'video/quicktime',
91 'mov' => 'video/quicktime',
92
93 // adobe
94 'pdf' => 'application/pdf',
95 'psd' => 'image/vnd.adobe.photoshop',
96 'ai' => 'application/postscript',
97 'eps' => 'application/postscript',
98 'ps' => 'application/postscript',
99
100 // ms office
101 'doc' => 'application/msword',
102 'rtf' => 'application/rtf',
103 'xls' => 'application/vnd.ms-excel',
104 'ppt' => 'application/vnd.ms-powerpoint',
105 'docx' => 'application/msword',
106 'xlsx' => 'application/vnd.ms-excel',
107 'pptx' => 'application/vnd.ms-powerpoint',
108
109 // open office
110 'odt' => 'application/vnd.oasis.opendocument.text',
111 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
112 );
113
114 if (array_key_exists($extension, $mimeTypes)){
115 return $mimeTypes[$extension];
116 } else {
117 return 'application/octet-stream';
118 }
119
120 }
121
122}