Skip to content

link: allow creating public link to files and folders - closes #1562 - #2176

Merged
breunigs merged 5 commits into
rclone:masterfrom
breunigs:issue-1562-link-to-files
Mar 29, 2018
Merged

breunigs merged 5 commits into
rclone:masterfrom
breunigs:issue-1562-link-to-files

Conversation

@breunigs

Copy link
Copy Markdown
Contributor

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 on Object as well. As it is now, it supports sharing directories. Getting dir sharing to work via Object would likely benefit from having a standardized Directory type 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.

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread cmd/link/link.go Outdated
`,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args)
fsrc, srcFileName := cmd.NewFsSrcWithFile(args)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/link/link.go Outdated
if err != nil {
return err
}
fmt.Printf("public link:\n%s\n", link)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably be most useful for scripting if it is just the link without the public link text.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread fs/operations/operations.go Outdated
}

// PublicLink adds a "readable by anyone with link" permission on the given file or folder.
func PublicLink(f fs.Fs, fileName string) (string, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fileName should probably be remote and be relative to the Fs root as per normal rclone usage.

Comment thread fs/fs.go Outdated
// 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fileName should probably be remote and be relative to the Fs root as per normal rclone usage.

@ncw

ncw commented Mar 24, 2018

Copy link
Copy Markdown
Member

Note that the Google Drive intergration test fails for me at TestObjectRemove, even on master.

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)
@breunigs

Copy link
Copy Markdown
Contributor Author

Writing a test case that ensures that f and f.Features().someFeature's receiver match seems rather tough. A friend more versed in Go came up with this https://play.golang.org/p/so-l-ZSPCSo, but this is clearly not nice, even if we'd just use it in tests.

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 NewFs patch into a separate commit, so that bisecting it later might be easier.

In the long run I wonder though if Features() and the optional interfaces can be replaced by making every interface required and backends return fs.ErrorNotImplemented if they don't support an optional feature. I lack a grand overview of rclone to be able to judge if this is feasible. Do you see any roadblocks with this approach that make even trying not worth it? I am not sure this approach would be better, or if it's just "bad in a different kind of way", though.

@ncw

ncw commented Mar 26, 2018

Copy link
Copy Markdown
Member

Writing a test case that ensures that f and f.Features().someFeature's receiver match seems rather tough. A friend more versed in Go came up with this https://play.golang.org/p/so-l-ZSPCSo, but this is clearly not nice, even if we'd just use it in tests.

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 getReceiver easily enough. So it would either a) work b) fail or c) panic any of which are acceptable outcomes for a test!

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 NewFs patch into a separate commit, so that bisecting it later might be easier.

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 getReceiver tests to the integration tests for each remote in due course?

Otherwise fine - merge when ready!

@breunigs

Copy link
Copy Markdown
Contributor Author

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!

@ncw

ncw commented Mar 27, 2018

Copy link
Copy Markdown
Member

Thanks @breunigs :-)

@breunigs
breunigs merged commit a8267d1 into rclone:master Mar 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants