This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackfs.py
executable file
·156 lines (120 loc) · 4.84 KB
/
slackfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
import os
import sys
import time
from collections import defaultdict
from pathlib import Path
from stat import S_IFDIR, S_IFREG
import logging
from tempfile import NamedTemporaryFile
import requests
import slack
from fuse import FUSE, Operations, FuseOSError
from fuse import LoggingMixIn
from errno import ENOENT
TOKEN = os.environ["SLACKFS_TOKEN"]
LOG_LEVEL = os.environ.get("SLACKFS_LOG_LEVEL", logging.ERROR)
class SlackFS(LoggingMixIn, Operations):
def __init__(self):
self.slack_client = slack.WebClient(token=TOKEN, proxy=os.environ.get("https_proxy", None))
self.channels = {channel["name_normalized"]: channel for channel in self.list_conversations()}
self.files = defaultdict(dict)
self.fd = 0
@staticmethod
def make_file_name(file_):
return f"{file_['id']}_{file_['name']}"
def list_conversations(self):
return self.slack_client.conversations_list(limit=200, types="public_channel,private_channel").data["channels"]
def channel_files(self, channel_name):
if channel_name not in self.files:
channel_id = self.channels[channel_name]["id"]
files = self.slack_client.files_list(channel=channel_id, limit=200).data["files"]
for f in files:
file_name = self.make_file_name(f)
self.files[channel_name][file_name] = f
return self.files[channel_name]
def get_file(self, channel_name, file_name):
self.channel_files(channel_name)
return self.files[channel_name][file_name]
def get_file_contents(self, channel_name, file_name):
f = self.get_file(channel_name, file_name)
if "contents" not in f:
r = requests.request("GET", f["url_private_download"], headers={"Authorization": f"Bearer {TOKEN}"})
f["contents"] = r.content
return bytes(f["contents"])
def getattr(self, path, fh=None):
p = Path(path)
mode = 0
size = 0
time_ = time.time()
if path == "/" or str(p.parent) == "/":
mode = S_IFDIR | 0o700
else:
try:
mode = S_IFREG | 0o600
file_ = self.get_file(channel_name=str(p.parent.name), file_name=str(p.name))
size = file_["size"]
time_ = file_["created"]
except KeyError:
raise FuseOSError(ENOENT)
return {
"st_atime": time_,
"st_ctime": time_,
"st_gid": os.getgid(),
"st_mode": mode,
"st_mtime": time_,
"st_nlink": 2,
"st_size": size,
"st_uid": os.getuid(),
}
def readdir(self, path, fh):
members = [".", ".."]
if path == "/":
members.extend(self.channels)
else:
channel_name = str(Path(path).name)
members.extend(self.channel_files(channel_name))
yield from iter(members)
def open(self, path, flags):
self.fd += 1
return self.fd
def read(self, path, length, offset, fh):
p = Path(path)
contents = self.get_file_contents(channel_name=str(p.parent.name), file_name=str(p.name))
return contents[offset : offset + length]
def create(self, path, mode):
p = Path(path)
self.fd += 1
self.files[p.parent.name][p.name] = {"contents": bytearray(), "size": 0, "created": time.time()}
return self.fd
def write(self, path, data, offset, fh):
p = Path(path)
file_ = self.get_file(p.parent.name, p.name)
contents = file_["contents"]
if len(contents) < offset + len(data):
contents += b"\0" * (offset + len(data) - len(contents))
file_["size"] = len(contents)
contents[offset : offset + len(data)] = data
return len(data)
def release(self, path, fh):
p = Path(path)
channel_name = p.parent.name
local_file_name = p.name
file_ = self.get_file(channel_name, local_file_name)
if "id" not in file_:
with NamedTemporaryFile() as f: # because slack doesn't want our in-memory bytes
f.write(file_["contents"])
f.flush()
channel_id = self.channels[channel_name]["id"]
resp = self.slack_client.files_upload(file=f.name, channels=channel_id, filename=local_file_name)
remote_file = resp.data["file"]
normalized_file_name = self.make_file_name(remote_file)
file_.update(remote_file)
self.files[channel_name][normalized_file_name] = file_
del self.files[channel_name][local_file_name]
return 0
def main(mountpoint):
FUSE(SlackFS(), mountpoint, nothreads=True, foreground=True)
if __name__ == "__main__":
logging.basicConfig(level=LOG_LEVEL)
main(sys.argv[1])