-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
139 lines (106 loc) · 3.33 KB
/
Copy pathapi.py
File metadata and controls
139 lines (106 loc) · 3.33 KB
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
import base64
import io
import uuid
import flask
import wav
app = flask.Flask(__name__)
store = {}
def new_id():
return base64.urlsafe_b64encode(uuid.uuid4().bytes)[:-2].decode('ascii')
class AudioFile:
def __init__(self, id, name, data):
self.id = id
self.name = name
self.data = data
def length(self):
return len(self.data)
@staticmethod
def href(id):
return '/audiofiles/{0}'.format(id)
def document(self):
return {
'_links': {
'self': {'href': self.href(self.id)},
'audiodata': {'href': AudioData.href(self.id)},
'audiofiles': {'href': AudioFiles.href()}
},
'name': self.name,
'length': self.length()
}
def embedded(self):
return {
'_links': {
'self': {'href': self.href(self.id)}
},
'name': self.name,
'length': self.length()
}
class AudioFiles:
def __init__(self, audiofiles):
self.audiofiles = audiofiles
def count(self):
return len(self.audiofiles)
@staticmethod
def href():
return '/audiofiles'
def document(self):
return {
'_links': {
'self': {'href': self.href()},
'upload': {'href': self.href()}
},
'count': self.count(),
'_embedded': {
'audiofile': [
audiofile.embedded() for audiofile in self.audiofiles
]
}
}
@app.route(AudioFiles.href(), methods=['GET'])
def list_audiofiles():
audiofiles = AudioFiles(store.values())
return flask.jsonify(audiofiles.document())
@app.route(AudioFiles.href(), methods=['POST'])
def upload_audiofile():
file = flask.request.files['file']
audiofile = AudioFile(new_id(), file.filename, file.read())
store[audiofile.id] = audiofile
return flask.jsonify(audiofile.document())
@app.route(AudioFile.href('<id>'), methods=['GET'])
def retrieve_audiofile(id):
mimetype = flask.request.accept_mimetypes.best
audiofile = store[id]
if mimetype == 'application/octet-stream':
return audiofile.data
else:
return flask.jsonify(audiofile.document())
class AudioData:
def __init__(self, id, samplerate, channels, bitspersample):
self.id = id
self.samplerate = samplerate
self.channels = channels
self.bitspersample = bitspersample
@staticmethod
def href(id):
return '{0}/data'.format(AudioFile.href(id))
def document(self):
return {
'_links': {
'self': {'href': self.href(self.id)},
'audiofile': {'href': AudioFile.href(self.id)},
'audiofiles': {'href': AudioFiles.href()}
},
'samplerate': self.samplerate,
'channels': self.channels,
'bitsPerSample': self.bitspersample
}
@app.route(AudioData.href('<id>'), methods=['GET'])
def retrieve_audiodata(id):
audiofile = store[id]
wavfile = wav.read(io.BytesIO(audiofile.data))
audiodata = AudioData(
id, wavfile.sample_rate, wavfile.channel_count, wavfile.bits_per_sample
)
return flask.jsonify(audiodata.document())
if __name__ == '__main__':
app.run(debug=True)