Exemple #1
0
 /**
  * Copy a file to another folder.
  *
  * @param String $file_id   Direcory entry id of the file to copy
  *                          (use 'flashed' to read ids from from flash
  *                          memory for a bulk operation)
  * @param mixed  $source_id Optional folder id to return to after
  *                          operation has succeeded
  */
 public function copy_action($file_id, $source_id = null)
 {
     if (!$this->full_access) {
         throw new AccessDeniedException();
     }
     PageLayout::setTitle(_('Datei kopieren'));
     if (Request::isPost()) {
         $folder_id = Request::option('folder_id');
         $folder = StudipDirectory::get($folder_id);
         if ($file_id === 'flashed') {
             $ids = Request::optionArray('file_id');
         } else {
             $ids = array($file_id);
         }
         FileHelper::checkAccess($ids);
         if ($this->checkCopyQuota($ids)) {
             foreach ($ids as $id) {
                 $source_id = $source_id ?: FileHelper::getParentId($file_id) ?: $this->context_id;
                 $entry = DirectoryEntry::find($id);
                 $folder->copy($entry->file, FileHelper::ExtendFilename($entry->name, _('Kopie')), $entry->description);
             }
             PageLayout::postMessage(MessageBox::success(_('Die ausgewählten Dateien wurden erfolgreich kopiert')));
         } else {
             PageLayout::postMessage(MessageBox::error(_('Der Kopiervorgang wurde abgebrochen, ' . 'da Ihnen nicht genügend freier Speicherplatz zur Verfügung steht')));
         }
         $this->redirect($this->url_for_parent_directory($ids));
         return;
     }
     $this->file_id = $file_id;
     $this->dir_tree = FileHelper::getDirectoryTree($this->context_id);
     if ($file_id === 'flashed') {
         $this->flashed = $this->flash['copy-ids'];
         $this->parent_id = $source_id;
         FileHelper::checkAccess($this->flashed);
     } else {
         $this->parent_id = FileHelper::getParentId($file_id) ?: $this->context_id;
         FileHelper::checkAccess($file_id);
     }
     $this->active_folders = array_keys(FileHelper::getBreadCrumbs($this->parent_id));
     try {
         $parent = new DirectoryEntry($this->parent_id);
         $this->parent_file_id = $parent->file->id;
     } catch (Exception $e) {
         $this->parent_file_id = $this->context_id;
     }
 }
Exemple #2
0
 /**
  * Create a new file by copying the contents of an existing
  * file into this directory under the given name. Unlike the
  * link() method, this creates a separate file. Returns the
  * directory entry.
  *
  * @param File $source  source file to copy
  * @param string $name  destination file name
  *
  * @return DirectoryEntry  created DirectoryEntry object
  */
 public function copy(File $source, $name, $description = '')
 {
     // Copy single file?
     if ($source->storage_id != '') {
         $new_entry = $this->createFile($name, $description);
         $new_file = $new_entry->file;
         // copy contents
         $source_fp = $source->open('rb');
         $dest_fp = $new_file->open('wb');
         stream_copy_to_stream($source_fp, $dest_fp);
         fclose($dest_fp);
         fclose($source_fp);
         // copy attributes
         $new_file->filename = $source->filename;
         $new_file->restricted = $source->restricted;
         $new_file->mime_type = $source->mime_type;
         $new_file->size = $source->size;
         $new_file->update();
         return $new_entry;
     }
     //COPY directory
     $newFolder = $this->mkdir($name, $description);
     // Todo: This probably could be more sormy
     $folder = StudipDirectory::get($newFolder->file_id);
     $folder->filename = $newFolder->name;
     $folder->store();
     foreach ($source->listFiles() as $entry) {
         $folder->copy($entry->file, $entry->name, $entry->description);
     }
     return $folder;
 }