How to make users to download files in Dropbox with Ruby? - ruby

I'm now trying to make a function that allows users to access to the link of the file to download.
The native link is https://www.dropbox.com/home/Apps/ringle_records?preview=lesson_id_11%3Auser_id_5.mp3 but, it is only accessible by admin ( for sure ). If I have a user who is eligible to know this link, then I would love to allow this user to click some button to download the file.
I am kind of bogged down when it comes to implementation. Because if I use the below code, it seems it stores data into the server.
def self.dropbox_download
contents, metadata = Drop_client.get_file_and_metadata('/lesson_id_12:user_id_7.mp3.mp3')
begin
File.open('filename.mp3', 'wb') do |file|
file.write(contents)
end
rescue
end
end
I want to make user to click something and the "mp3" file will be downloaded, but I don't think the above code works in this way.
Please shoot any opinion! I'm looking forward to seeing!
Best

If you only need to allow other users to download this specific file (or some specific set of files), the easiest way is to just get a shared link to the relevant file(s):
https://www.dropbox.com/help/167
You can modify these shared links for different behaviors, e.g., for file downloads, as shown here:
https://www.dropbox.com/help/201
If you do need to get these links programmatically, you can use the shares method:
https://dropbox.github.io/dropbox-sdk-ruby/api-docs/v1.6.4/DropboxClient.html#method-i-shares
You can then give the shared link to the user, e.g., as a link on a web page, or in an email, etc.

Related

Google Drive API v3 : there isn't any way to get a download url for a google document?

The Google Drive API v2 to v3 migration guide says:
The exportLinks field has been removed from files. To export Google Documents, use the files.export method instead.
I don't want to export (download) the file right away. "files.export" will actually download the file. I want a link to download the file, later. This was possible in v2 by means of the exportLinks.
How can I in v3 accomplish the same? If it is not possible, why was this useful feature removed?
Besides, (similar problem to above) downloadUrl was also removed, and the suggested alternative ("files.get with ?alt=media") downloads the file instead of providing a download link. This means there is no way in v3 to get a public short lived URL for a file?
EDIT:
there is no way in v3 to get a public short lived URL for a file?
For regular files, apparently yes.
This seems to work fine (a public short lived link to the file with its right name and contents):
https://www.googleapis.com/drive/v3/files/ID?alt=media&access_token=TOKEN
For google apps files, no (not even private, as v2 exportLinks used to be).
https://www.googleapis.com/drive/v3/files/ID/exportmimeType=TYPEv&access_token=TOKEN
Similar to regular files, this URL is a short lived link to the file contents, but lacking of its right name.
BTW, I see the API is not behaving consistently: /drive/v3/files/FILEID delivers the right file name, but /drive/v3/files/FILEID/export does not.
I think the API itself should be setting the right Content-Disposition, as it is apparently doing when issuing a /drive/v3/files/FILEID call.
This file naming problem invalidates the workaround to the lack of ExportLinks in v3.
The v2 ExportLinks allowed me to link a file (which is not the same as getting its content right away). Anyone logged in and with the proper permissions was able to access it, and the link didn't needed any access_token, and it wasn't short lived. It was good and useful.
Building a link with a raw API call like /drive/v3/files/FILEID/export (with mandatory access_token) would be an close enough workaround (it is temporary and public, not the same as it was, anyway). However, the naming problem invalidates it.
In v2, regular files have a WebContentLink and google apps files have exportLinks. In v3 exportLinks are gone, and I don't see any suitable alternative to them.
Once you query for your file by id you can use the function getWebContentLink() to get the download link of the file (eg. $file->getWebContentLink() ).
I think you're placing too much emphasis on the word "method".
There is still a link to export a file, it's https://www.googleapis.com/drive/v3/files/fileIdxxxxx/export&mimeType=xxxxx/xxxxx. Make sure you URL encode the mime type.
Eg
https://www.googleapis.com/drive/v3/files/1fGBQ81haNU_nEiC5GITZD3bxT0ppL2LHg-C0ubD4Q_s/export?mimeType=text/csv&access_token=ya29.Gmo0BMvO-pVEPKsiD9j4D-NZVGE91MChRvwOcBSg3cTHt5uAClf-jFxcovQScbO2QQhwHS95eSGW1eQQcK5G1UQ6oI4BFEJJkntEBkgriZ14GbHuvpDL7LT2pKA--WiPuNoDDIuZMm5lWtlr
These links form part of the API, so the expectation is that you've written a client that sends authenticated requests, and deals with the response data. This explains why, if you simply paste the link into a browser without an access_token, it will fail. It also explains why the filename is export, ie. it isn't intended that your client would ever use a filename, but rather it should receive the data as a stream. This SO answer discusses the situation in more detail How to set name of file downloaded from browser?

windows folder for temporary files created by my application

I'm trying to decide where the 'correct folder' to store documents and logs created by my windows form application. The application is used in education and has all paths held in the SQL server. Some (like logs file paths are shared) are accessible on network but specifically for temporary documents where should I default the storage to? I've recently tried the Users/username/AppData/ folder but I seem to be getting differing results after installation; so far I have put this down to people user credentials as often in schools they can do whatever they want (yes I know shocking indeed).
If anyone can point me in the direction of an MSDn article or knows better please reply - Thanks.
** Edit 10/09/2013 - Sorry all I should be further explicit. I'm looking for the folder / structure Microsoft has designed for this sort off activity. My application already provides users with the ability to create thier own working directories (there are several required) but I'm keen to use the 'correct' locations for this sort of activity... I thought the right place would be c:/Users/USERNAME/Appdata/APPLICATION FOLDER/ but as I mention I've come across a few access rights issues when uses install the application.... hope that explains better - thanks
To create temporary directory you can use something like this:
public string GetTempDirectory() {
string path = Path.GetRandomFileName();
Path.Combine(Path.GetTempPath(), path);
Directory.CreateDirectory(path);
return path;
}
Path class info
Directory class info

How to view drupal 7 sessions from an external PHP page?

I installed drupal 7 and I need to include a block of PHP code in the user profile page.
I created a new block and I put this code in it:
"include (http://mysite/sites/all/themes/campi_aggiuntivi.php)"
In "campi_aggiuntivi.php" I put the following code:
session_start ();
print_r ($ _SESSION);
But it returns an empty array.
How do I interact with drupal sessions in this external page?
Thank you very much for your answares.
Thank you all for your answers.
I'm trying to install a module and insert it into a block avege as I suggested.
I created the file test.info
name = Test Block
description = description Test Block
package = package Test Block
core = 7.x
dependencies[] = block
; Information added by drupal.org packaging script on 2013-02-01
version = "7.x-1.x-dev"
core = "7.x"
project = "Test Block"
datestamp = "1359680350"
But I can not figure out which function I put in the file test.module, to insert the test module management screen Blocks
Can you help me please?
Thank you again to all
I think you need to include Drupal Bootstrap and a few other necessary procedures. Here's a link to an issue on d.o that might help.
1st thing i want to suggest is don't include like this:
"include (http://mysite/sites/all/themes/campi_aggiuntivi.php)"
Use local path instead: "include (sites/all/themes/campi_aggiuntivi.php)"
But this depends on from where you are calling, if both the files are at same location(calling in user profile tpl file) then this might work: "include (campi_aggiuntivi.php)"
I presume you need session details to figure-out the user id, that information can be retrieved from $profile variable or arg(1)
I have not tested your code but the way you are trying to do is not the conventional way to extend Drupal functionality.
Also i hope print_r ($ _SESSION); is actually print_r ($_SESSION); in your code, i.e. without spaces.
If you want to add a block in user profile page, i recommend doing it the drupal way.
Drupal already has an excellent example for creating a block programmatically. Here is the link.
Have a look at block_example module inside it. It will give you relevant example codes that you can use to create the block.
The example also contains information about how to make the block appear in certain pages by default.
After you create the block place them in the region you would want to, like left sidebar, content etc of the relevant theme that is enabled on user profile pages.
Inside your custom module you would access to $_SESSION variable.
From here on you can include the code in campi_aggiuntivi.php inside your module itself.
Or use the following foolproof method to load the file. This would even work when you call it from a theme.
Say you want to use custom_module_name.campi_aggiuntivi.inc located in your custom module, from a theme then you can use module_load_include
For example,
module_load_include('inc', 'custom_module_name', 'custom_module_name.campi_aggiuntivi');
the above call would load custom_module_name.campi_aggiuntivi.inc from custom_module_name module folder. This way you don't have to hard code the path required to access the file.
Based on your update.. it sounds like you are now trying to create a block programmatically. There are example modules that you can download on Drupal.org. There is a block module in that download.

DocsList findFolder() issue

This is a google spreadsheet script question.
I have a GUI setup in order to search for "SouthWest" and then find a "test" sheet. This is the code I am using.
var file = DocsList.getFolder("SouthWest").find("test");
This works just fine when I run it under my account (as I have this folder and file setup correctly) but when another user is logged into google docs it will attempt to search for this folder/file under the new user instead of the owner of the document. Is there a way to have it just search the DocsList of the owner of the spreadsheet that is currently open? The error that I get under the new user is "Error encountered: Cannot find folder SouthWest." Thanks.
If you always want to access the same file, you can use the getFileById method and address it directly instead of searching every time:
https://developers.google.com/apps-script/class_docslist#getFileById
Of course, you should make sure that all users are allowed to access that file.

Launching a registered mime helper application

I used to be able to launch a locally installed helper application by registering a given mime-type in the Windows registry. This enabled me to allow users to be able to click once on a link to the current install of our internal browser application. This worked fine in Internet Explorer 5 (most of the time) and Firefox but now does not work in Internet Explorer 7.
The filename passed to my shell/open/command is not the full physical path to the downloaded install package. The path parameter I am handed by IE is
"C:\Document and Settings\chq-tomc\Local Settings\Temporary Internet Files\
EIPortal_DEV_2_0_5_4[1].expd"
This unfortunately does not resolve to the physical file when calling FileExists() or when attempting to create a TFileStream object.
The physical path is missing the Internet Explorer hidden caching sub-directory for Temporary Internet Files of "Content.IE5\ALBKHO3Q" whose absolute path would be expressed as
"C:\Document and Settings\chq-tomc\Local Settings\Temporary Internet Files\
Content.IE5\ALBKHO3Q\EIPortal_DEV_2_0_5_4[1].expd"
Yes, the sub-directories are randomly generated by IE and that should not be a concern so long as IE passes the full path to my helper application, which it unfortunately is not doing.
Installation of the mime helper application is not a concern. It is installed/updated by a global login script for all 10,000+ users worldwide. The mime helper is only invoked when the user clicks on an internal web page with a link to an installation of our Desktop browser application. That install is served back with a mime-type of "application/x-expeditors". The registration of the ".expd" / "application/x-expeditors" mime-type looks like this.
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.expd]
#="ExpeditorsInstaller"
"Content Type"="application/x-expeditors"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\ExpeditorsInstaller]
"EditFlags"=hex:00,00,01,00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\ExpeditorsInstaller\shell]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\ExpeditorsInstaller\shell\open]
#=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\ExpeditorsInstaller\shell\open\command]
#="\"C:\\projects\\desktop2\\WebInstaller\\WebInstaller.exe\" \"%1\""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MIME\Database\Content Type\application/x-expeditors]
"Extension"=".expd"
I had considered enumerating all of a user's IE cache entries but I would be concerned with how long it may take to examine them all or that I may end up finding an older cache entry before the current entry I am looking for. However, the bracketed filename suffix "[n]" may be the unique key.
I have tried wininet method GetUrlCacheEntryInfo but that requires the URL, not the virtual path handed over by IE.
My hope is that there is a Shell function that given a virtual path will hand back the physical path.
I believe the sub-directories created by IE are randomly generated, so you won't be able guarantee that it will be named the same every time, and the problem I see with the registry method is that it only works when the file is still in the cache...emptying the cache would purge the file requiring yet another installation.
Would it not be better to install this helper into application data?
I'm not sure about this but perhaps this may lead you in the right direction: try using URL cache functions from the wininet DLL: FindFirstUrlCacheEntry, FindNextUrlCacheEntry, FindCloseUrlCache for enumeration and when you locate an entry whose local file name matches the given path maybe you can use RetrieveUrlCacheEntryFile to retrieve the file.
I am using a similar system with the X-Appl browser to display WAML web applications and it works perfectly. Maybe you should have a look at how they managed to do it.
It looks like iexplore is passing the shell namespace "name" of the file rather than the filesystem name.
I dont think there is a documented way to be passed a shell item id on the command line - explorer does it to itself, but there are marshaling considerations as shell item ids are (pointers to) binary data structures that are only valid in a single process.
What I might try doing is:
1. Call SHGetDesktopFolder which will return the root IShellFolder object of the shell namespace.
2. Call the IShellFolder::ParseDisplayName to turn the name you are given back into a shell item id list.
3. Try the IShellFolder::GetDisplayNameOF with the SHGDN_FORPARSING flag - which, frankly, feels like w'eve just gone in a complete circle and are back where we started. Because I think its this API thats ultimately responsible for returning the "wrong" filesystem relative path.
Some follow-up to close out this question.
Turned out the real issue was how I was creating the file handle using TFileStream. I changed to open with fmOpenRead or fmShareDenyWrite which solved what turned out to be a file locking issue.
srcFile := TFileStream.Create(physicalFilename, fmOpenRead or fmShareDenyWrite);

Resources