Add message download

This commit is contained in:
Ian Kent 2014-04-26 11:50:34 +01:00
parent c9d12d1fee
commit 73088ab400
2 changed files with 39 additions and 1 deletions

View file

@ -28,6 +28,7 @@ func CreateAPIv1(exitCh chan int, conf *config.Config, server *http.Server) *API
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/delete/?$"), apiv1.delete_all)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/?$"), apiv1.message)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/delete/?$"), apiv1.delete_one)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/download/?$"), apiv1.download)
return apiv1
}
@ -70,8 +71,40 @@ func (apiv1 *APIv1) message(w http.ResponseWriter, r *http.Request, route *handl
w.Header().Set("Content-Type", "text/json")
w.Write(bytes)
default:
// FIXME 404?
w.Header().Set("Content-Type", "text/json")
w.Write([]byte("[]"))
w.Write([]byte("{}"))
}
}
func (apiv1 *APIv1) download(w http.ResponseWriter, r *http.Request, route *handler.Route) {
match := route.Pattern.FindStringSubmatch(r.URL.Path)
id := match[1]
log.Printf("[APIv1] GET /api/v1/messages/%s/download\n", id)
w.Header().Set("Content-Type", "message/rfc822")
w.Header().Set("Content-Disposition", "attachment; filename=\"" + id + ".eml\"")
switch apiv1.config.Storage.(type) {
case *storage.MongoDB:
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
for h, l := range message.Content.Headers {
for _, v := range l {
w.Write([]byte(h + ": " + v + "\r\n"))
}
}
w.Write([]byte("\r\n" + message.Content.Body))
case *storage.Memory:
message, _ := apiv1.config.Storage.(*storage.Memory).Load(id)
for h, l := range message.Content.Headers {
for _, v := range l {
w.Write([]byte(h + ": " + v + "\r\n"))
}
}
w.Write([]byte("\r\n" + message.Content.Body))
default:
// FIXME 404?
w.Write([]byte(""))
}
}
@ -84,6 +117,8 @@ func (apiv1 *APIv1) delete_all(w http.ResponseWriter, r *http.Request, route *ha
apiv1.config.Storage.(*storage.MongoDB).DeleteAll()
case *storage.Memory:
apiv1.config.Storage.(*storage.Memory).DeleteAll()
default:
// FIXME 404?
}
}
@ -98,5 +133,7 @@ func (apiv1 *APIv1) delete_one(w http.ResponseWriter, r *http.Request, route *ha
apiv1.config.Storage.(*storage.MongoDB).DeleteOne(id)
case *storage.Memory:
apiv1.config.Storage.(*storage.Memory).DeleteOne(id)
default:
// FIXME 404?
}
}

View file

@ -100,6 +100,7 @@ func Index() string {
</td>
<td>
<button class="btn btn-xs btn-default" title="Delete" ng-click="deleteOne(message)"><span class="glyphicon glyphicon-remove"></span></button>
<a class="btn btn-xs btn-default" title="Delete" href="/api/v1/messages/{{ message.Id }}/download"><span class="glyphicon glyphicon-save"></span></a>
</td>
</tr>
</tbody>