Read the word document content from google drive - laravel

I am trying fetch doc content from google drive using the Google sdk. The problem is, the get() function is not working properly. It gives only file metadata not any actual content.
$googl = new Googl();
$this->client = $googl->client();
$this->client->setAccessToken(session('user.token'));
$this->drive = $googl->drive($this->client);
$file = $this->drive->files->get("fileId");
return $file;

From the documentation it looks like you can download a file using:
$file = $this->drive->files->get("fileId", array('alt' => 'media'));
$content = $file->getBody()->getContents();

I suspect that we may have a language issue here.
Read the content from a Google drive file
The google drive api is a file datastore api. This means that it stores the file itself and the information about said file. It does not give you any access to the contents of that file.
If you want to know what is in the file you will need to download the file and open it up on your machine.
or try Google documents api however i suspect that it will only be able to read a google drive document file and not a word file you may have to convert it first.
download the the file
If you check the documentation for Download Files you will find the following.
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$response = $driveService->files->get($fileId, array(
'alt' => 'media'));
$content = $response->getBody()->getContents();

Related

Open an image in Google Docs

Just a little background: As you might know, if you have an image in Google drive and you try to open it with Google Docs, Google tries to extract the text within your image (OCR) and show the text alongside the image in a newly created Google Docs file.
I am writing a script and within that script I would like to open an image (with some text) with Google Docs.
When I try to do it through the following code, I just an Error message with no explanation on why it fails.
var file_url = file.getUrl();
try
{
var doc = DocumentApp.openByUrl(file_url);
}catch (e) {
Logger.log("Error Occurred: " + e.toString());
}
Any help would be appreciated.
Edit 1: Here is an actual scenario. I have the following .png image and I can open it with Google Docs.
When I open with Google Docs, I get the following document:
Document generated after opening the .png image with Google Doc.
Having the url of this .png file, I would like to do all this through my script so I can have the OCR generated text.
You want to convert a PNG file to Google Document by OCR using Google Apps Script.
If my understanding is correct, how about this modification? In this modification, Drive API is used.
When you use this script, please enable Drive API at Advanced Google Services and API console. You can see about this at here.
Modified script:
var fileId = "### file ID of PNG file ###";
var file = DriveApp.getFileById(fileId);
Drive.Files.insert(
{title: file.getName(), mimeType: MimeType.GOOGLE_DOCS},
file.getBlob(),
{ocr: true}
);
Note:
When the URL of PNG file is like https://drive.google.com/file/d/#####/view?usp=sharing, ##### is the file ID.
In this modified script, the filename of created Document uses the filename of file retrieved by fileId. If you want to use other filename, please modify title: file.getName().
References:
Advanced Google Services
Drive API

Empty Shared with me Folder in Google Drive?

I want to Empty Share with me folder How can I empty It is My Code and I am using Google Drive V3 .
service.Permissions.Delete(PermissionID, fileId).Execute();
service.Files.Delete(fileId).Execute();
Both line gives a permission 403 error.
If I delete MyDrive file that time Second line he worked fine but Shared With me Folder not Deleted
The thing you need to remember is that Share with me is not a file you actually own that is why this didn't work.
service.Files.Delete(fileId).Execute();
First Get a list of all files in Share with me folder.
var request = service.Files.List();
request.Q = "(sharedWithMe = true)";
request.Fields = "*";
var results = request.Execute();
Find the file you wish to delete:
var myfile = results.Files.Where(a => a.Name.ToLower().Equals("receipt.pdf")).FirstOrDefault();
Now find the permissions on that file associated with the current authenticated user:
var per = myfile.Permissions.Where(a => a.EmailAddress.ToLower().Equals("xxxxx#gmail.com")).FirstOrDefault();
Delete the permissions from the mail file.
service.Permissions.Delete(myfile.Id, per.Id).Execute();
I tested it and it worked. You can just run the initial request though a loop and delete everything if you wish.
Note: This does not appear to work in all cases. I have a file on my Google drive that was shared with me by what appears to be a service account. I have no permissions on the file there for i cant remove my access. I am still digging.

How to download file from google drive api with service account?

Hello google hackers!
I am using Drive Service app and uploaded file successfully like this:
require 'googleauth'
require 'google/apis/drive_v2'
Drive = Google::Apis::DriveV2
upload_source = "/User/my_user_name/hacking.txt"
drive = Drive::DriveService.new
# Drive::AUTH_DRIVE is equal to https://www.googleapis.com/auth/drive
drive.authorization = Google::Auth.get_application_default([Drive::AUTH_DRIVE])
file = drive.insert_file({title: 'hacking.txt'}, upload_source: upload_source)
file has a lot of properties, like this:
download_url
But when I try to open this download_url in browser it shows me blank screen. Why I can't download it?
I guess, that may be there are permission problems? But the scope is correct, and uploading is successful...
The answer is simple - we cannot download it from file object, we must send another get request, just download it like this:
drive.get_file(file.id, download_dest: '/tmp/my_file.txt')

Is it possible to upload image or file to SkyDrive fom Metro Style App?

Is it possible to upload image or file to SkyDrive fom Metro Style App?
I have already found how to browse the file from SkyDrive. But I haven't found regarding uploading file to SkyDrive. If you reply me, it will be very thankful..
I don't think the file picker method works unless the user has the desktop app installed.
You should use a Sharing contract. If you add a data file (Storage Item) to share, then SkyDrive will be listed as a share target and the user gets a UI where they can choose where in their SkyDrive they want to save. This is how I implemented it in my app.
For more info...
http://msdn.microsoft.com/en-us/library/windows/apps/hh771179.aspx
You can use FileSavePicker to save files. This will of course give the user a chance to select where he wants to save to local documents folder or sky drive. The user is in control.
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
savePicker.DefaultFileExtension = ".YourExtension";
savePicker.SuggestedFileName = "SampleFileName";
savePicker.FileTypeChoices[".YourExtension"] = new List<string>() { ".YourExtension"};
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
await FileIO.WriteTextAsync(file, "A bunch of text to save to the file");
}
Please note that in the sample code I am creating the content of the file in code. If you want the user to select an existing file from the computer then you will have to first use FileOpenPicker, get the file and then use FileSavePicker to save the contents of the selected file to the SkyDrive
Assuming that you are using XAML/JavaScript, the suggested solution is to use FilePicker.
The following link may help you.
http://msdn.microsoft.com/en-us/library/windows/apps/jj150595.aspx
Thanks Mamta Dalal and Dangling Neuron, but there is problem. But it looks like I can't use FileSavePicker. I have to upload file(documnet, photo) not only text file. I have to copy from one path to another. If I use FileSavePicker, I have to write every file content (text, png, pdf, etc) and can't copy. Currently I am using FolderPicker. But unfortunately, FolderPicker doesn't support SkyDrive.My Code is As follow:
>FolderPicker saveFolder = new FolderPicker();
>saveFolder.ViewMode = PickerViewMode.Thumbnail;
>saveFolder.SuggestedStartLocation = PickerLocationId.Desktop;
>saveFolder.FileTypeFilter.Add("*");
>StorageFolder storagefolderSave = await saveFolder.PickSingleFolderAsync();
>StorageFile storagefileSave = [Selected storagefile with file picker];
>await storagefileSave.CopyAsync(storagefolderSave,storagefileSave.Name,NameCollisionOption.ReplaceExisting);
It will be greate that if FolderPicker supports SkyDrive or can copy file using FileSavePicker.

How to upload a file in joomla?

Hi i am making a simple component in joomla having name image detail and i have to upload that image how can i upload image from backend. which one is better using extension or make custom. can you please share any good article for it. i have searched many more but due to lack of idea on joomla cannot find. hope you genius guys help me.
thanks i advance
Joomla Component for the exact scenario of your requirement will be very hard to find out. So you've two options:
1. Make your own component
2. Customize other similar type of component like gallery component
For uploading file from joomla component on admin if you're making your own component:
1. Just use move_uploaded_file php function.
2. copy this code, for joomla's standard fxn :
function upload($src, $dest)
{
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
$ret = false;
$dest = JPath::clean($dest);
$baseDir = dirname($dest);
if (!file_exists($baseDir)) {
jimport('joomla.filesystem.folder');
JFolder::create($baseDir);
}
if ($FTPOptions['enabled'] == 1) {
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
$dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
if (is_uploaded_file($src) && $ftp->store($src, $dest))
{
$ret = true;
unlink($src);
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
}
} else {
if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors
if (JPath::setPermissions($dest)) {
$ret = true;
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR01'));
}
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
}
}
return $ret;
}
If you want to use other's component and edit it according to need, download it:
http://prakashgobhaju.com.np/index.php?option=com_showcase_gallery&view=items&catid=1&Itemid=64
Remember it's a gallery component.
Uploading any file be it an image on your Joomla site is something which is so simple, and can be done using either the web based FTP and or the desktop FTP services like filezilla but only when you have saved the file you want to upload. Using the web based way, you need to log in to your host for example 000webhost, locate the file manager option, click on it and enter your domain username and password. Then go to public_html folder , create a new folder for your photos or images and click on upload. Locate your image and click on the tick link to start uploading.
Using the desktop way, you will need to unzip your file to add to joomla, open your FTP client like filezilla, locate the file on local host, input your log in details as provided by your host and once you are logged in to your account through filezilla, locate where you want to add the file and click on upload.
You can find a similar tutorial with regard here http://www.thekonsulthub.com/how-tos/how-to-upload-joomla-with-filezilla-to-your-hosting-servers-cpanel/ {entire thing}
Please please please make sure you use the filtering available in the MediaHelper. Specifically never trust uploaded images, always check first that they are valid file types, then that they are in the list of approved types of files listed in your global configuration, that the names do not contain html or javascript, and that the files themselves do not contain code. In particular I would recommend the MediaHelper::canUpload method which will check the majority of these things for you. If anything you should be checking even more strongly. Also make sure that you are checking whether the user has permission to upload. If anything you should make the checking even more restrictive. Use the APIs that joomla gives you, such as the built in media field.

Resources