From 67816da0bfe16ecb58d3a3bdb70c1fb9a79cff75 Mon Sep 17 00:00:00 2001
From: Georg Ehrke <georg@ownCloud.com>
Date: Tue, 11 Jun 2013 13:15:24 +0200
Subject: [PATCH] save current work state of office fallback

---
 lib/preview/msoffice.php     | 95 ++++++++++++++++++++++++++----------
 lib/preview/opendocument.php | 12 +++++
 2 files changed, 81 insertions(+), 26 deletions(-)

diff --git a/lib/preview/msoffice.php b/lib/preview/msoffice.php
index c99ca313c7..4fedb735c9 100644
--- a/lib/preview/msoffice.php
+++ b/lib/preview/msoffice.php
@@ -7,22 +7,24 @@
  */
 namespace OC\Preview;
 
-class MSOffice2003 extends Provider {
+class DOC extends Provider {
 
-	public function getMimeType(){
-		return null;
+	public function getMimeType() {
+		return '/application\/msword/';
 	}
 
-	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview){
-		return false;
+	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
+		
 	}
+
 }
 
+\OC\Preview::registerProvider('OC\Preview\DOC');
 
-class MSOffice2007 extends Provider {
+class DOCX extends Provider {
 
-	public function getMimeType(){
-		return null;
+	public function getMimeType() {
+		return '/application\/vnd.openxmlformats-officedocument.wordprocessingml.document/';
 	}
 
 	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
@@ -39,36 +41,50 @@ class MSOffice2007 extends Provider {
 
 		unlink($tmpdoc);
 
-		//new image object
 		$image = new \OC_Image($pdf);
-		//check if image object is valid
-		if (!$image->valid()) return false;
-
-		return $image;
+		
+		return $image->valid() ? $image : false;
 	}
+
 }
 
-class DOC extends MSOffice2003 {
+\OC\Preview::registerProvider('OC\Preview\DOCX');
+
+class MSOfficeExcel extends Provider {
 
 	public function getMimeType() {
-		return '/application\/msword/';
+		return null;
 	}
 
-}
+	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
+		require_once('PHPExcel/Classes/PHPExcel.php');
+		require_once('PHPExcel/Classes/PHPExcel/IOFactory.php');
+		//require_once('mpdf/mpdf.php');
 
-\OC\Preview::registerProvider('OC\Preview\DOC');
+		$abspath = $fileview->toTmpFile($path);
+		$tmppath = \OC_Helper::tmpFile();
 
-class DOCX extends MSOffice2007 {
+		$rendererName = \PHPExcel_Settings::PDF_RENDERER_DOMPDF;
+		$rendererLibraryPath = \OC::$THIRDPARTYROOT . '/dompdf';
 
-	public function getMimeType() {
-		return '/application\/vnd.openxmlformats-officedocument.wordprocessingml.document/';
+		\PHPExcel_Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
+
+		$phpexcel = new \PHPExcel($abspath);
+		$excel = \PHPExcel_IOFactory::createWriter($phpexcel, 'PDF');
+		$excel->save($tmppath);
+
+		$pdf = new \imagick($tmppath . '[0]');
+		$pdf->setImageFormat('jpg');
+
+		unlink($abspath);
+		unlink($tmppath);
+
+		return $image->valid() ? $image : false;
 	}
 
 }
 
-\OC\Preview::registerProvider('OC\Preview\DOCX');
-
-class XLS extends MSOffice2003 {
+class XLS extends MSOfficeExcel {
 
 	public function getMimeType() {
 		return '/application\/vnd.ms-excel/';
@@ -78,7 +94,7 @@ class XLS extends MSOffice2003 {
 
 \OC\Preview::registerProvider('OC\Preview\XLS');
 
-class XLSX extends MSOffice2007 {
+class XLSX extends MSOfficeExcel {
 
 	public function getMimeType() {
 		return '/application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet/';
@@ -88,7 +104,34 @@ class XLSX extends MSOffice2007 {
 
 \OC\Preview::registerProvider('OC\Preview\XLSX');
 
-class PPT extends MSOffice2003 {
+class MSOfficePowerPoint extends Provider {
+
+	public function getMimeType() {
+		return null;
+	}
+
+	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
+		//require_once('');
+		//require_once('');
+
+		$abspath = $fileview->toTmpFile($path);
+		$tmppath = \OC_Helper::tmpFile();
+
+		$excel = PHPPowerPoint_IOFactory::createWriter($abspath, 'PDF');
+		$excel->save($tmppath);
+
+		$pdf = new \imagick($tmppath . '[0]');
+		$pdf->setImageFormat('jpg');
+
+		unlink($abspath);
+		unlink($tmppath);
+
+		return $image->valid() ? $image : false;
+	}
+
+}
+
+class PPT extends MSOfficePowerPoint {
 
 	public function getMimeType() {
 		return '/application\/vnd.ms-powerpoint/';
@@ -98,7 +141,7 @@ class PPT extends MSOffice2003 {
 
 \OC\Preview::registerProvider('OC\Preview\PPT');
 
-class PPTX extends MSOffice2007 {
+class PPTX extends MSOfficePowerPoint {
 
 	public function getMimeType() {
 		return '/application\/vnd.openxmlformats-officedocument.presentationml.presentation/';
diff --git a/lib/preview/opendocument.php b/lib/preview/opendocument.php
index e69de29bb2..786a038ff8 100644
--- a/lib/preview/opendocument.php
+++ b/lib/preview/opendocument.php
@@ -0,0 +1,12 @@
+<?php
+/**
+ * Copyright (c) 2013 Georg Ehrke georg@ownCloud.com
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+namespace OC\Preview;
+
+class OpenDocument {
+	
+}
\ No newline at end of file
-- 
GitLab