Exemple #1
0
 /**
  * Get called by 'post_write' hook (file creation, file update)
  * @param \OCP\Files\Node $file the file
  */
 public function update($file, $userId)
 {
     // debug logging
     $this->logger->log('update - ' . $file->getPath(), 'debug');
     if (!$file instanceof \OCP\Files\File) {
         return;
     }
     $mimetype = $file->getMimeType();
     // debug logging
     $this->logger->log('update - mimetype ' . $mimetype, 'debug');
     $this->emit('\\OCA\\Music\\Utility\\Scanner', 'update', array($file->getPath()));
     if (substr($mimetype, 0, 5) === 'image') {
         $coverFileId = $file->getId();
         $parentFolderId = $file->getParent()->getId();
         $this->albumBusinessLayer->updateCover($coverFileId, $parentFolderId);
         return;
     }
     if (substr($mimetype, 0, 5) !== 'audio' && substr($mimetype, 0, 15) !== 'application/ogg') {
         return;
     }
     if (ini_get('allow_url_fopen')) {
         // TODO find a way to get this for a sharee
         $isSharee = $userId && $this->userId !== $userId;
         $musicPath = $this->configManager->getUserValue($this->userId, $this->appName, 'path');
         if ($musicPath !== null || $musicPath !== '/' || $musicPath !== '') {
             // TODO verify
             $musicPath = $this->userFolder->get($musicPath)->getPath();
             // skip files that aren't inside the user specified path (and also for sharees - TODO remove this)
             if (!$isSharee && substr($file->getPath(), 0, strlen($musicPath)) !== $musicPath) {
                 $this->logger->log('skipped - outside of specified path', 'debug');
                 return;
             }
         }
         $fileInfo = $this->extractor->extract('oc://' . $file->getPath());
         $hasComments = array_key_exists('comments', $fileInfo);
         if (!$hasComments) {
             // TODO: fix this dirty fallback
             // fallback to local file path removed
             $this->logger->log('fallback metadata extraction - removed code', 'debug');
             // $fileInfo = $this->extractor->extract($this->api->getLocalFilePath($path));
             // $hasComments = array_key_exists('comments', $fileInfo);
         }
         // artist
         $artist = null;
         if ($hasComments && array_key_exists('artist', $fileInfo['comments'])) {
             $artist = $fileInfo['comments']['artist'][0];
             if (count($fileInfo['comments']['artist']) > 1) {
                 $this->logger->log('multiple artists found (use shortest): ' . implode(', ', $fileInfo['comments']['artist']), 'debug');
                 // determine shortest, because the longer names are just concatenations of all artists
                 for ($i = 0; $i < count($fileInfo['comments']['artist']); $i++) {
                     if (strlen($fileInfo['comments']['artist'][$i]) < strlen($artist)) {
                         $artist = $fileInfo['comments']['artist'][$i];
                     }
                 }
             }
         }
         if ($artist === '') {
             // assume artist is not set
             $artist = null;
         }
         $alternativeTrackNumber = null;
         // title
         $title = null;
         if ($hasComments && array_key_exists('title', $fileInfo['comments'])) {
             $title = $fileInfo['comments']['title'][0];
         }
         if ($title === null || $title === '') {
             // fallback to file name
             $title = $file->getName();
             if (preg_match('/^(\\d+)\\W*[.-]\\W*(.*)/', $title, $matches) === 1) {
                 $alternativeTrackNumber = $matches[1];
                 if (preg_match('/(.*)(\\.(mp3|ogg))$/', $matches[2], $titleMatches) === 1) {
                     $title = $titleMatches[1];
                 } else {
                     $title = $matches[2];
                 }
             }
         }
         // album
         $album = null;
         if ($hasComments && array_key_exists('album', $fileInfo['comments'])) {
             $album = $fileInfo['comments']['album'][0];
         }
         if ($album === '' || $album === null) {
             // album name not set in fileinfo, use parent folder name as album name
             if ($this->userFolder->getId() === $file->getParent()->getId()) {
                 // if the file is in user home, still set album name to unknown
                 $album = null;
             } else {
                 $album = $file->getParent()->getName();
             }
         }
         // track number
         $trackNumber = null;
         if ($hasComments && array_key_exists('track_number', $fileInfo['comments'])) {
             $trackNumber = $fileInfo['comments']['track_number'][0];
         }
         if ($trackNumber === null && $alternativeTrackNumber !== null) {
             $trackNumber = $alternativeTrackNumber;
         }
         // convert track number '1/10' to '1'
         $tmp = explode('/', $trackNumber);
         $trackNumber = $tmp[0];
         // check for numeric values - cast them to int and verify it's a natural number above 0
         if (is_numeric($trackNumber) && (int) $trackNumber > 0) {
             $trackNumber = (int) $trackNumber;
         } else {
             $trackNumber = null;
         }
         $year = null;
         if ($hasComments && array_key_exists('year', $fileInfo['comments'])) {
             $year = $fileInfo['comments']['year'][0];
             if (!ctype_digit($year)) {
                 $year = null;
             }
         }
         $fileId = $file->getId();
         $length = null;
         if (array_key_exists('playtime_seconds', $fileInfo)) {
             $length = ceil($fileInfo['playtime_seconds']);
         }
         $bitrate = null;
         if (array_key_exists('audio', $fileInfo) && array_key_exists('bitrate', $fileInfo['audio'])) {
             $bitrate = $fileInfo['audio']['bitrate'];
         }
         // debug logging
         $this->logger->log('extracted metadata - ' . sprintf('artist: %s, album: %s, title: %s, track#: %s, year: %s, mimetype: %s, length: %s, bitrate: %s, fileId: %i, this->userId: %s, userId: %s', $artist, $album, $title, $trackNumber, $year, $mimetype, $length, $bitrate, $fileId, $this->userId, $userId), 'debug');
         if (!$userId) {
             $userId = $this->userId;
         }
         // add artist and get artist entity
         $artist = $this->artistBusinessLayer->addArtistIfNotExist($artist, $userId);
         $artistId = $artist->getId();
         // add album and get album entity
         $album = $this->albumBusinessLayer->addAlbumIfNotExist($album, $year, $artistId, $userId);
         $albumId = $album->getId();
         // add track and get track entity
         $track = $this->trackBusinessLayer->addTrackIfNotExist($title, $trackNumber, $artistId, $albumId, $fileId, $mimetype, $userId, $length, $bitrate);
         // debug logging
         $this->logger->log('imported entities - ' . sprintf('artist: %d, album: %d, track: %d', $artistId, $albumId, $track->getId()), 'debug');
     }
 }