Ejemplo n.º 1
0
 public function testFromFile()
 {
     $file = $this->getMockBuilder('OCP\\Files\\File')->getMock();
     $file->expects($this->any())->method('getId')->will($this->returnValue(3));
     $file->expects($this->any())->method('getContent')->will($this->returnValue('content'));
     $file->expects($this->any())->method('getMTime')->will($this->returnValue(323));
     $file->expects($this->any())->method('getName')->will($this->returnValue('file.txt'));
     $note = Note::fromFile($file);
     $this->assertEquals(3, $note->getId());
     $this->assertEquals(323, $note->getModified());
     $this->assertEquals('file', $note->getTitle());
     $this->assertEquals('content', $note->getContent());
 }
Ejemplo n.º 2
0
 /**
  * Updates a note. Be sure to check the returned note since the title is
  * dynamically generated and filename conflicts are resolved
  * @param int $id the id of the note used to update
  * @param string $content the content which will be written into the note
  * @param string $handwrittenContent json content from the handwriting feature
  * the title is generated from the first line of the content
  * @throws NoteDoesNotExistException if note does not exist
  * @return \OCA\Notes\Db\Note the updated note
  */
 public function update($id, $content, $userId, $handwrittenContent = "")
 {
     $folder = $this->getFolderForUser($userId);
     $file = $this->getFileById($folder, $id);
     $handwrittenFile = $this->getHandwrittenContent($id, $userId);
     $handwrittenFile->putContent($handwrittenContent);
     // generate content from the first line of the title
     $splitContent = explode("\n", $content);
     $title = $splitContent[0];
     if (!$title) {
         $title = $this->l10n->t('New note');
     }
     // prevent directory traversal
     $title = str_replace(array('/', '\\'), '', $title);
     // using a maximum of 100 chars should be enough
     $title = substr($title, 0, 100);
     // generate filename if there were collisions
     $currentFilePath = $file->getPath();
     $basePath = '/' . $userId . '/files/Notes/';
     $fileExtension = ".txt";
     $newFilePath = $basePath . $this->generateFileName($folder, $title, $fileExtension, $id);
     // if the current path is not the new path, the file has to be renamed
     if ($currentFilePath !== $newFilePath) {
         $file->move($newFilePath);
     }
     $file->putContent($content);
     \OCP\Util::writeLog('notes', print_r(Note::fromFile($file), true), \OCP\Util::ERROR);
     return Note::fromFile($file, $handwrittenFile->getContent());
 }