Skip to content

Commit ac1ceac

Browse files
committed
feat: add support for "shared with me"
1 parent aeaf307 commit ac1ceac

6 files changed

Lines changed: 63 additions & 55 deletions

File tree

.goreleaser.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ nfpms:
7070
dst: /usr/lib/systemd/user/adfinis-rclone-mgr@.service
7171
- src: ./assets/rclone@.service
7272
dst: /usr/lib/systemd/user/rclone@.service
73+
- src: ./assets/adfinis-rclone-wrapper.sh
74+
dst: /usr/bin/adfinis-rclone-wrapper.sh
7375
- src: ./assets/google_drive_opener.py
7476
dst: /usr/share/nautilus-python/extensions/google_drive_opener.py
7577
- src: ./assets/adfinis-rclone-mgr.desktop

assets/adfinis-rclone-wrapper.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
INSTANCE="$1"
4+
MOUNT_POINT="$HOME/google/$INSTANCE"
5+
CACHE_DIR="$HOME/.cache/google/$INSTANCE"
6+
7+
CMD=(/usr/bin/rclone mount \
8+
--cache-dir "$CACHE_DIR" \
9+
--vfs-cache-mode writes \
10+
--vfs-cache-max-size 10G)
11+
12+
if [[ "$INSTANCE" == "Shared_With_Me" ]]; then
13+
CMD+=(--drive-shared-with-me)
14+
fi
15+
16+
CMD+=("$INSTANCE:" "$MOUNT_POINT")
17+
18+
# Execute the command
19+
exec "${CMD[@]}"

assets/google_drive_opener.py

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,43 @@ def get_file_items(self, *args):
5252

5353
return items
5454

55+
def _get_rclone_file(self, file_path):
56+
try:
57+
relative_path = os.path.relpath(file_path, self.RCLONE_MOUNT_PATH)
58+
drive_name = relative_path.split(os.sep)[0]
59+
# remove drive_name and the last part of the path
60+
file_path = os.path.join("", *relative_path.split(os.sep)[1:-1])
61+
file_name = os.path.basename(relative_path)
62+
63+
cmd = ['rclone', 'lsjson', f'{drive_name}:{file_path}']
64+
# special case for shared_with_me
65+
if drive_name.lower() == "shared_with_me":
66+
cmd.append('--drive-shared-with-me')
67+
68+
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
69+
files = json.loads(result.stdout)
70+
71+
if not files:
72+
raise FileNotFoundError("No files returned from rclone lsjson")
73+
74+
# find matching file by name
75+
files = [f for f in files if f['Path'] == file_name]
76+
if not files:
77+
raise FileNotFoundError(f"File '{file_name}' not found in Google Drive")
78+
79+
# assuming the first file is the one we want
80+
file = files[0]
81+
return file
82+
83+
except subprocess.CalledProcessError as e:
84+
subprocess.Popen(["zenity", "--error", "--text", f"rclone failed:\n{e.stderr}"])
85+
except Exception as e:
86+
subprocess.Popen(["zenity", "--error", "--text", f"Unexpected error:\n{str(e)}"])
87+
5588
def open_rclone_url(self, menu, file_paths):
5689
for file_path in file_paths:
5790
try:
58-
relative_path = os.path.relpath(file_path, self.RCLONE_MOUNT_PATH)
59-
drive_name = relative_path.split(os.sep)[0]
60-
# remove drive_name and the last part of the path
61-
file_path = os.path.join("", *relative_path.split(os.sep)[1:-1])
62-
file_name = os.path.basename(relative_path)
63-
64-
cmd = ['rclone', 'lsjson', f'{drive_name}:{file_path}']
65-
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
66-
files = json.loads(result.stdout)
67-
68-
if not files:
69-
raise FileNotFoundError("No files returned from rclone lsjson")
70-
71-
# find matching file by name
72-
files = [f for f in files if f['Path'] == file_name]
73-
if not files:
74-
raise FileNotFoundError(f"File '{file_name}' not found in Google Drive")
75-
76-
# assuming the first file is the one we want
77-
file = files[0]
91+
file = self._get_rclone_file(file_path)
7892
file_id = file['ID']
7993

8094
# send warning if file is open document format
@@ -84,8 +98,6 @@ def open_rclone_url(self, menu, file_paths):
8498
url = f'https://drive.google.com/open?id={file_id}'
8599
webbrowser.get("xdg-open").open(url)
86100

87-
except subprocess.CalledProcessError as e:
88-
subprocess.Popen(["zenity", "--error", "--text", f"rclone failed:\n{e.stderr}"])
89101
except Exception as e:
90102
subprocess.Popen(["zenity", "--error", "--text", f"Unexpected error:\n{str(e)}"])
91103

@@ -99,32 +111,9 @@ def _copy_to_clipboard(self, text):
99111
def copy_file_link(self, menu, file_paths):
100112
file_path = file_paths[0]
101113
try:
102-
relative_path = os.path.relpath(file_path, self.RCLONE_MOUNT_PATH)
103-
drive_name = relative_path.split(os.sep)[0]
104-
# remove drive_name and the last part of the path
105-
file_path = os.path.join("", *relative_path.split(os.sep)[1:-1])
106-
file_name = os.path.basename(relative_path)
107-
108-
cmd = ['rclone', 'lsjson', f'{drive_name}:{file_path}']
109-
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
110-
files = json.loads(result.stdout)
111-
112-
if not files:
113-
raise FileNotFoundError("No files returned from rclone lsjson")
114-
115-
# find matching file by name
116-
files = [f for f in files if f['Path'] == file_name]
117-
if not files:
118-
raise FileNotFoundError(f"File '{file_name}' not found in Google Drive")
119-
120-
# assuming the first file is the one we want
121-
file = files[0]
114+
file = self._get_rclone_file(file_path)
122115
file_id = file['ID']
123-
124116
url = f'https://drive.google.com/open?id={file_id}'
125117
self._copy_to_clipboard(url)
126-
127-
except subprocess.CalledProcessError as e:
128-
subprocess.Popen(["zenity", "--error", "--text", f"rclone failed:\n{e.stderr}"])
129118
except Exception as e:
130-
subprocess.Popen(["zenity", "--error", "--text", f"Unexpected error:\n{str(e)}"])
119+
subprocess.Popen(["zenity", "--error", "--text", f"Unexpected error:\n{str(e)}"])

assets/rclone@.service

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ AssertPathIsDirectory="%h/google/%I"
88

99
[Service]
1010
Type=notify
11-
ExecStart=/usr/bin/rclone mount \
12-
--cache-dir "%h/.cache/google/%I" \
13-
--vfs-cache-mode writes \
14-
--vfs-cache-max-size 10G \
15-
"%I:" "%h/google/%I"
11+
ExecStart=/usr/bin/adfinis-rclone-wrapper.sh %I
1612
ExecStop=/bin/fusermount -u "%h/google/%I"
1713

1814
[Install]

gdrive_config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ func newHttpHandler(ctx context.Context, cancel context.CancelFunc) *http.ServeM
251251
})
252252
}
253253

254-
fmt.Println(result)
255-
256254
if err := handleRcloneConfig(ctx, result, clientID, clientSecret, string(tokenValue)); err != nil {
257255
log.Printf("Failed to handle rclone config: %v", err)
258256
w.WriteHeader(http.StatusInternalServerError)
@@ -298,6 +296,10 @@ func checkAvailableDrives(ctx context.Context, oauthConfig *oauth2.Config, token
298296
Name: "My Drive",
299297
ID: "my_drive",
300298
},
299+
{
300+
Name: "Shared With Me",
301+
ID: "shared_with_me",
302+
},
301303
}
302304
pageToken := ""
303305
for {

rclone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func handleRcloneConfig(ctx context.Context, drives []models.Drive, clientID, cl
2626
if drive.Enabled {
2727
var configMap rc.Params
2828
// special case for personal drive
29-
if drive.ID == "my_drive" {
29+
if drive.ID == "my_drive" || drive.ID == "shared_with_me" {
3030
configMap = rc.Params{
3131
"type": "drive",
3232
"root_folder_id": "",

0 commit comments

Comments
 (0)