Download and preview from Google docs directly

Phoca Download - download manager
crishead
Phoca Newbie
Phoca Newbie
Posts: 1
Joined: 21 May 2012, 16:59

Download and preview from Google docs directly

Post by crishead »

Hello,

the google docs is a very popular cloud system to managed files (big free disk space, easy use, etc). I modified a little the code in phoca download, and now I can download and preview the files from google docs.
this code find in the /components/com_phocadownload/views/category/tmpl/default_files.php

The code begin in the 24 row (between then //General and //pdtextonly):

Code: Select all

			// General
			$linkDownloadB = '';
			$linkDownloadE = '';
			if ((int)$v->confirm_license > 0 || $this->tmpl['display_file_view'] == 1) {
				$linkDownloadB = '<a href="'. JRoute::_(PhocaDownloadHelperRoute::getFileRoute($v->id, $v->catid,$v->alias, $v->categoryalias, $v->sectionid). $this->tmpl['limitstarturl']).'" >';	// we need pagination to go back			
				$linkDownloadE ='</a>';
			} else {
				if ($v->link_external != '' && $v->directlink == 1) {
					$linkDownloadB = '<a href="'.$v->link_external.'" target="'.$this->tmpl['download_external_link'].'" >';
					$linkDownloadE ='</a>';
					//Google docs link
					$linkURL = parse_url($v->link_external);
					if ($linkURL['host'] == 'docs.google.com') {
						$extqueryParts = explode('&', $linkURL['query']);
						$extparams = array();
						foreach ($extqueryParts as $extparam) {
							$extitem = explode('=', $extparam);
							$extparams[$extitem[0]] = $extitem[1];
						}
						if ($extparams['id'] != '') {
							$linkDownloadB = '<a href="https://docs.google.com/uc?export=download&id='.$extparams['id'].'">';
							$linkDownloadE ='</a>';
						}
					} 
				} else {
					$linkDownloadB = '<a href="'. JRoute::_(PhocaDownloadHelperRoute::getFileRoute($v->id,$this->category[0]->id,$v->alias, $this->category[0]->alias, $v->sectionid, 'download').$this->tmpl['limitstarturl']).'" >';
					$linkDownloadE ='</a>';
				}
			}
			


and the preview section begin in the //pdfbuttonpreview to //pdfbuttondownload:

Code: Select all

			$pdButtonPreview = '';
			if (isset($v->filename_preview) && $v->filename_preview != '') {
				$fileExt = PhocaDownloadHelper::getExtension($v->filename_preview);
				if ($fileExt == 'pdf' || $fileExt == 'jpeg' || $fileExt == 'jpg' || $fileExt == 'png' || $fileExt == 'gif') {
		
					$filePath	= PhocaDownloadHelper::getPathSet('filepreview');
					$filePath	= str_replace ( '../', JURI::base(true).'/', $filePath['orig_rel_ds']);
					$previewLink = $filePath . $v->filename_preview;	
					$pdButtonPreview	.= '<div class="pd-button-preview">';
					
					if ($this->tmpl['preview_popup_window'] == 1) {
						$pdButtonPreview .= '<a  href="'.$previewLink.'" onclick="'. $this->buttonpr->options.'" >'. JText::_('COM_PHOCADOWNLOAD_PREVIEW').'</a>';
					} else {	
						if ($fileExt == 'pdf') {
							// Iframe - modal
							$pdButtonPreview .= '<a class="pd-modal-button" href="'.$previewLink.'" rel="'. $this->buttonpr->options.'" >'. JText::_('COM_PHOCADOWNLOAD_PREVIEW').'</a>';
						} else {
							// Image - modal
							$pdButtonPreview .= '<a class="pd-modal-button" href="'.$previewLink.'" rel="'. $this->buttonpr->optionsimg.'" >'. JText::_('COM_PHOCADOWNLOAD_PREVIEW').'</a>';
						}
					}
					$pdButtonPreview	.= '</div>';
				} 
				else {
					if (stristr($v->filename_preview,'docs.google.com')) {
						$linkURL = parse_url($v->filename_preview);
							$extqueryParts = explode('&', $linkURL['query']);
							$extparams = array();
							foreach ($extqueryParts as $extparam) {
								$extitem = explode('=', $extparam);
								$extparams[$extitem[0]] = $extitem[1];
							}
							if ($extparams['id'] != '') {
								$previewLink =  "https://docs.google.com/viewer?authuser=0&srcid=".$extparams['id']."&pid=explorer&a=v&chrome=false&embedded=true";							
								$pdButtonPreview	.= '<div class="pd-button-preview">';
									// Iframe - modal
									$pdButtonPreview .= '<a class="pd-modal-button" href="'.$previewLink.'" rel="'. $this->buttonpr->options.'" >'. JText::_('COM_PHOCADOWNLOAD_PREVIEW').'</a>';
								$pdButtonPreview	.= '</div>';
							}
					
					}
				}
			}
			
I'm not a programmer, possible this code not optimal, but my server is functionally. I hope, I can help for you.

USAGE:
upload a document (e.g. a pdf file to your google docs account)
change this file permission to public (e.g. only link)
click the share... button or menu
copy the share link (e.g. https://docs.google.com/open?id=.................................) to external file link and preview file name fields in the phoca download file section.

WARNING:
this code tested only my system, please make a backup your original default_files.php !!!

Best regards, crishead
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48595
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Download and preview from Google docs directly

Post by Jan »

Hi, thank you for this guide and information, I will take a look at it - to test it.

Thank you, Jan
If you find Phoca extensions useful, please support the project
Ariota
Phoca Newbie
Phoca Newbie
Posts: 3
Joined: 27 Jun 2012, 11:30

Re: Download and preview from Google docs directly

Post by Ariota »

Hello and thanks for the guide, I tried but I do not work, adding the link to the preview google doc on my site disappears the Preview button.
Post Reply