No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZipFile.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * SCORMBundle/Entity//ZipFile.php
  4. *
  5. * @category ScormEngine_Entity
  6. * @author Cédric Leberche <cedric.leberche@Logipro\Bundle.com>
  7. */
  8. namespace Logipro\Bundle\SCORMBundle\Entity;
  9. use Logipro\Bundle\SCORMBundle\Entity\ZipFile;
  10. use Logipro\Bundle\SCORMBundle\Entity\MapCourseZipfile;
  11. use Logipro\Bundle\SCORMBundle\LearningModels\DOMSCORM;
  12. use Logipro\Bundle\SCORMBundle\Entity\Common\AbstractEntity;
  13. use Logipro\Bundle\SCORMBundle\LearningModels\DOMLearningModel;
  14. use Logipro\Bundle\SCORMBundle\Package\Exception\PackageException;
  15. /**
  16. * Représente un fichier compressé
  17. *
  18. * @author Cédric Leberche <cedric.leberche@Logipro\Bundle.com>
  19. */
  20. class ZipFile extends AbstractEntity
  21. {
  22. const IMPORT_STATUS_NOT_IMPORTED = 'NOT_IMPORTED';
  23. const IMPORT_STATUS_PROCESSING = 'PROCESSING';
  24. const IMPORT_STATUS_IMPORTED = 'IMPORTED';
  25. const IMPORT_STATUS_ERROR = 'ERROR';
  26. /**
  27. * Id du fichier
  28. *
  29. * @var int
  30. */
  31. protected $zipFileId;
  32. /**
  33. * Nom du fichier
  34. *
  35. * @var string
  36. */
  37. protected $zipFileName;
  38. /**
  39. * Empreinte du fichier
  40. *
  41. * @var string
  42. */
  43. protected $zipFileprint;
  44. /**
  45. * Date du téléchargement
  46. *
  47. * @var \DateTime
  48. */
  49. protected $uploadDate;
  50. /**
  51. * Date de fin de téléchargement
  52. *
  53. * @var \DateTime
  54. */
  55. protected $endUploadDate;
  56. /**
  57. *
  58. * @var string
  59. */
  60. protected $standard = DOMSCORM::SCORM_2004;
  61. /**
  62. * Status d'import
  63. *
  64. * @var string
  65. */
  66. protected $importStatus = self::IMPORT_STATUS_NOT_IMPORTED;
  67. /**
  68. *
  69. * @var ArrayCollection
  70. */
  71. protected $mapCourseZipfiles;
  72. //Getters and setters
  73. /**
  74. * Get the value of $zipFileId
  75. *
  76. * @return int
  77. */
  78. public function getZipFileId()
  79. {
  80. return $this->zipFileId;
  81. }
  82. /**
  83. * Set the value of $zipFileId
  84. *
  85. * @param int $zipFileId Id à
  86. * attribuer
  87. *
  88. * @return self
  89. */
  90. public function setZipFileId(int $zipFileId)
  91. {
  92. $this->zipFileId = $zipFileId;
  93. return $this;
  94. }
  95. /**
  96. * Get the value of $zipFileName
  97. *
  98. * @return string
  99. */
  100. public function getZipFileName()
  101. {
  102. return $this->zipFileName;
  103. }
  104. /**
  105. * Set the value of $zipFileName
  106. *
  107. * @param string $zipFileName Nom de fichier à
  108. * attribuer
  109. *
  110. * @return self
  111. */
  112. public function setZipFileName(string $zipFileName)
  113. {
  114. $this->zipFileName = $zipFileName;
  115. return $this;
  116. }
  117. /**
  118. * Get the value of $zipFileprint
  119. *
  120. * @return string
  121. */
  122. public function getZipFileprint()
  123. {
  124. return $this->zipFileprint;
  125. }
  126. /**
  127. * Set the value of $zipFileprint
  128. *
  129. * @param string $zipFileprint Empreinte du fichier à
  130. * attribuer
  131. *
  132. * @return self
  133. */
  134. public function setZipFileprint(string $zipFileprint)
  135. {
  136. $this->zipFileprint = $zipFileprint;
  137. return $this;
  138. }
  139. /**
  140. * Get the value of $uploadDate
  141. *
  142. * @return \DateTime
  143. */
  144. public function getUploadDate()
  145. {
  146. return $this->uploadDate;
  147. }
  148. /**
  149. * Set the value of $uploadDate
  150. *
  151. * @param \DateTime $uploadDate Date du téléchargement à
  152. * attribuer
  153. *
  154. * @return self
  155. */
  156. public function setUploadDate($uploadDate)
  157. {
  158. $this->uploadDate = $uploadDate;
  159. return $this;
  160. }
  161. /**
  162. * Get the value of $endUploadDate
  163. *
  164. * @return \DateTime
  165. */
  166. public function getEndUploadDate()
  167. {
  168. return $this->EndUploadDate;
  169. }
  170. /**
  171. * Set the value of $endUploadDate
  172. *
  173. * @param \DateTime $endUploadDate Date de fin du téléchargement à
  174. * attribuer
  175. *
  176. * @return self
  177. */
  178. public function setEndUploadDate($endUploadDate)
  179. {
  180. $this->endUploadDate = $endUploadDate;
  181. return $this;
  182. }
  183. /**
  184. * Get the value of $standard
  185. *
  186. * @return string
  187. */
  188. public function getStandard()
  189. {
  190. return $this->standard;
  191. }
  192. /**
  193. * Set the value of $standard
  194. *
  195. * @param string $standard
  196. * @return self
  197. */
  198. public function setStandard($standard)
  199. {
  200. $this->standard = $standard;
  201. return $this;
  202. }
  203. /**
  204. *
  205. * Get the value of $importStatus
  206. *
  207. * @return string
  208. */
  209. public function getImportStatus()
  210. {
  211. if ($this->importStatus === null) {
  212. return self::IMPORT_STATUS_NOT_IMPORTED;
  213. }
  214. return $this->importStatus;
  215. }
  216. /**
  217. * Set the value of $importStatus
  218. *
  219. * @param string $importStatus
  220. *
  221. * @return self
  222. */
  223. public function setImportStatus($importStatus)
  224. {
  225. $this->importStatus = $importStatus;
  226. return $this;
  227. }
  228. /**
  229. * Add MapCourseZipfile entity to collection (one to many).
  230. *
  231. * @param \Logipro\Bundle\SCORMBundle\Entity\ScormEngine\MapCourseZipfile $mapCourseZipfiles
  232. * @return \Logipro\Bundle\SCORMBundle\Entity\ScormEngine\ZipFile
  233. */
  234. public function addMapCourseZipfile(MapCourseZipfile $mapCourseZipfile)
  235. {
  236. $this->mapCourseZipfiles[] = $mapCourseZipfile;
  237. return $this;
  238. }
  239. /**
  240. * Remove MapCourseZipfile entity from collection (one to many).
  241. *
  242. * @param \Logipro\Bundle\SCORMBundle\Entity\ScormEngine\MapCourseZipfile $mapCourseZipfiles
  243. * @return \Logipro\Bundle\SCORMBundle\Entity\ScormEngine\ZipFile
  244. */
  245. public function removeMapCourseZipfile(MapCourseZipfile $mapCourseZipfile)
  246. {
  247. $this->mapCourseZipfiles->removeElement($mapCourseZipfile);
  248. return $this;
  249. }
  250. /**
  251. * Get MapCourseZipfile entity collection (one to many).
  252. *
  253. * @return \Doctrine\Common\Collections\Collection
  254. */
  255. public function getMapCourseZipfiles()
  256. {
  257. return $this->mapCourseZipfiles;
  258. }
  259. /**
  260. * Téléchargement d'un fichier zip
  261. *
  262. * @param [type] $file variable de type $_FILES
  263. *
  264. * @return boolean
  265. */
  266. public function uploadZipFile($file)
  267. {
  268. if ($_FILES["tmpFile"]["error"] == 0) {
  269. // Récupère le fichier zip et le place dans le dossier zipFile
  270. $uploadDate = new \DateTime('NOW');
  271. $fileName = $_FILES["tmpFile"]["name"];
  272. $filePath = "../public/uploads/zipFile/".$fileName;
  273. $tmp = $_FILES["tmpFile"]["tmp_name"];
  274. if (move_uploaded_file($tmp, $filePath)) {
  275. $this->importStatus = self::IMPORT_STATUS_PROCESSING;
  276. }
  277. $endUploadDate = new \DateTime('NOW');
  278. $this->zipFileName = $fileName;
  279. $this->zipFileprint = md5_file($filePath);
  280. $this->uploadDate = $uploadDate;
  281. $this->endUploadDate = $endUploadDate;
  282. $this->filePath = $filePath;
  283. return true;
  284. } else {
  285. return false;
  286. }
  287. }
  288. /**
  289. * Upload d'un fichier zip à partir d'un chemin
  290. *
  291. * @param string $zipPath
  292. * @param string $uploadPath
  293. *
  294. * @return array
  295. */
  296. public function uploadZipFromPath(string $zipPath, string $uploadPath)
  297. {
  298. $response = array();
  299. $response['existingFile'] = null;
  300. $response['upload'] = false;
  301. if (file_exists($zipPath)) {
  302. // Récupération de la signature du zip
  303. $hash = md5_file($zipPath);
  304. // Test de l'existance de la signature
  305. $repository = $this->entityManager->getRepository(ZipFile::class);
  306. $oldZipFile = $repository->findOneBy(['zip_fileprint' => $hash]);
  307. // si le fichier existe déjà, on renvoie son id
  308. if (!empty($oldZipFile)) {
  309. $response['existingFile'] = $oldZipFile;
  310. return $response;
  311. }
  312. //upload du fichier coté moteur
  313. $uploadDate = new \DateTime('NOW');
  314. $fileName = pathinfo($zipPath)['filename'];
  315. $filePath = $uploadPath . $fileName;
  316. if (copy($zipPath, $filePath)) {
  317. $this->importStatus = self::IMPORT_STATUS_PROCESSING;
  318. }
  319. $endUploadDate = new \DateTime('NOW');
  320. $this->zipFileName = $fileName;
  321. $this->zipFileprint = $hash;
  322. $this->uploadDate = $uploadDate;
  323. $this->endUploadDate = $endUploadDate;
  324. $this->filePath = $filePath;
  325. $response['upload'] = true;
  326. }
  327. return $response;
  328. }
  329. /**
  330. * Extraire le fichier zip vers le répertoire $directory
  331. *
  332. * @param string $directory
  333. *
  334. * @return void
  335. */
  336. public function extractZipFile($directory)
  337. {
  338. $zip = new \ZipArchive();
  339. $openResult = $zip->open($this->filePath);
  340. if ($openResult == false) {
  341. throw new PackageException(sprintf("Impossible d'ouvrir %s", $this->filePath));
  342. }
  343. $extractResult = $zip->extractTo($directory);
  344. if ($extractResult == false) {
  345. throw new PackageException(sprintf("Impossible d'extraire %s", $directory));
  346. }
  347. $zip->close();
  348. }
  349. /**
  350. * renvoi le DOMSCORM du paquet
  351. *
  352. * @return DOMSCORM
  353. */
  354. public function getDOM() : DOMSCORM
  355. {
  356. $dom = DOMLearningModel::getInstance($this->zipFileId);
  357. if ($dom == null) {
  358. $XMLManifest = DOMLearningModel::getManifest($this->zipFileName);
  359. $dom = DOMLearningModel::setDOM($this->zipFileId, $XMLManifest);
  360. }
  361. return $dom;
  362. }
  363. }