Repository navigation
link: allow creating public link to files and folders - closes #1562 - #2176
Conversation
ncw
left a comment
There was a problem hiding this comment.
Nice idea and an often requested feature :-)
I put some thoughts inline.
It could probably also do with a column in the overview table and maybe another issue to collect up the remotes which aren't done yet.
Potentially this can be used by a lot of remotes.. S3/Swift/GCS/AzureBlob can make buckets public. I'm pretty sure box can have sharing links and probably all the others can too.
I can see as soon as we release this we'll have requests for the time limited links and the other optional features!
| `, | ||
| Run: func(command *cobra.Command, args []string) { | ||
| cmd.CheckArgs(1, 1, command, args) | ||
| fsrc, srcFileName := cmd.NewFsSrcWithFile(args) |
There was a problem hiding this comment.
I would have thought you'd be better calling newFsFile and then you'll know whether the argument points to a file or directory without the need for a trailing / check which I've tried to avoid in rclone. (You'd have to make it public first though...)
Then the PublicLink is then either sharing the root directory (if passed "") or a path within that directory.
I don't know - what do you think?
There was a problem hiding this comment.
You are most certainly right, but I ran into issues previously and it was not clear to me where they were coming from. With Dropbox, I can simply drop in NewFsFile and it continues to work.
However, with Drive I run into the peculiar issue that the f.root changes. I added some debug output in the form of fmt.Printf("%s @ %s 0\n", f.Root(), remote) to the code. It's still fine in operations.PublicLink. It's fine at the top of Dropbox's PublicLink. However, looking at the debug output for Drive (xxlink is a file at the very root, i.e. no subroot):
$ go build -i && ./rclone -vvv link Gsuite:xxlink
2018/03/24 19:36:01 DEBUG : rclone: Version "v1.40-DEV" starting with parameters ["./rclone" "-vvv" "link" "Gsuite:xxlink"]
2018/03/24 19:36:01 DEBUG : Using config file from "/home/stefan/.rclone.conf"
@ xxlink
@ xxlink 0
xxlink @ xxlink 1
2018/03/24 19:36:02 DEBUG : Google drive root 'xxlink': attempting to share single file 'xxlink'
xxlink @ xxlink 2
drive fs: Google drive root 'xxlink'
2018/03/24 19:36:02 Failed to link: object not found
For comparison, this is the output for Dropbox (again rclone139 is a top level file):
$ go build -i && ./rclone -vvv link dropbox:rclone139
2018/03/24 19:35:01 DEBUG : rclone: Version "v1.40-DEV" starting with parameters ["./rclone" "-vvv" "link" "dropbox:rclone139"]
2018/03/24 19:35:01 DEBUG : Using config file from "/home/stefan/.rclone.conf"
@ rclone139
@ rclone139 0
@ rclone139 1
2018/03/24 19:35:02 DEBUG : Dropbox root '': attempting to share 'rclone139' (absolute path: /rclone139)
2018/03/24 19:35:02 DEBUG : /rclone139: has a public link already, attempting to retrieve it
https://www.dropbox.com/s/otsnp322yzdetup/rclone139?dl=0
2018/03/24 19:35:03 DEBUG : 4 go routines active
2018/03/24 19:35:03 DEBUG : rclone: Version "v1.40-DEV" finishing with parameters ["./rclone" "-vvv" "link" "dropbox:rclone139"]
So I guess there must be a bug somewhere in the drive code? Maybe rootFolderId or some of the dirCache is not cleaned properly… if you have a hunch, please let me know.
There was a problem hiding this comment.
Hm, it seems that the old or incorrect FS resurfaces somehow… I print the memory addresses in drive's NewFs implementation here: https://github.com/ncw/rclone/blob/master/backend/drive/drive.go#L571
in NewFs:
old: 0xc4203b4000 (f)
new: 0xc42057a090 (newF)
in operations.PublicLink:
0xc42057a090 (i.e. the correct one)
in drive.PublicLink:
0xc4203b4000 (i.e. the incorrect one)
so I guess Features() hangs on to the old reference somehow?
There was a problem hiding this comment.
So, it's exactly like that: the f features are filled with functions whose receiver is *f. These are copied when newF is created and are never updated – they keep *f as their receiver. I guess calling .Fill() again might work, but so does simply overwriting f's dirCache and root.
A test that ensures that the address of f matches the addresses of all receivers of f.Features() functions sounds like a decent idea. Unfortunately I could not find a way to get the pointer of a func's receiver from the func's own pointer.
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Printf("public link:\n%s\n", link) |
There was a problem hiding this comment.
This would probably be most useful for scripting if it is just the link without the public link text.
There was a problem hiding this comment.
Just putting the link out there seemed wrong, and there always is tail -n1. On second thought the user pretty much expects a link, so the explanation is superfluous. I've removed it.
| } | ||
|
|
||
| // PublicLink adds a "readable by anyone with link" permission on the given file or folder. | ||
| func PublicLink(f fs.Fs, fileName string) (string, error) { |
There was a problem hiding this comment.
This fileName should probably be remote and be relative to the Fs root as per normal rclone usage.
| // PublicLinker is an optional interface for Fs | ||
| type PublicLinker interface { | ||
| // PublicLink generates a public link to the remote path (usually readable by anyone) | ||
| PublicLink(fileName string) (string, error) |
There was a problem hiding this comment.
This fileName should probably be remote and be relative to the Fs root as per normal rclone usage.
I had noticed that, its been showing up in the integration tests... I'll work in that in #2177 |
Note: this also fixes a bug in NewFs where an Fs instance would be different from the one the optional features functions were referring to. For more details, please see rclone#2176 (comment)
|
Writing a test case that ensures that For the current patch, I'd like to merge it as it is now. Well, if you think it's helpful I can move drive's In the long run I wonder though if |
That is very clever! Not particularly appetising as it relies on an implementation detail of go, but we could make it reliable enough for the tests, by writing a test for
Can you split the drive patch out - it can stay in this PR - that would be great. I think it would be worth making a new issue to reference it with as I bet there are other remotes with the same problem. Were you thinking that we'd add the Otherwise fine - merge when ready! |
|
Split off the receiver issues into #2182. I will put the Drive patch there. Let's see if I can make a durable enough test, though! |
|
Thanks @breunigs :-) |
For now only Google Drive and Dropbox are included.
The goal of this command is to quickly get a sharable link to hand to friends. Drive supports full ACLs, Dropbox offers password protection and automatic expiry (paid accounts). Not sure if it makes sense to implement these provider specific features. The most likeliest feature to add would probably to "unshare", which could be equally universal.
It's an optional interface for
Fs, although it would probably fit nicely onObjectas well. As it is now, it supports sharing directories. Getting dir sharing to work viaObjectwould likely benefit from having a standardizedDirectorytype first. I haven't surveyed the other storage providers yet, so maybe demanding directory sharing through the integration tests will have to change.Note that the Google Drive intergration test fails for me at
TestObjectRemove, even on master.There is one edge case in the Drive implementation that I left unsolved: on a team drive, if I lack permissions (i.e.
canShare: false) this implementation will bail. Instead, it could check what permissions the file/dir already has, and return the link if they are already sufficient. I'd revisit this if someone actually runs into this issue.As always, I am happy to address any feedback.