Skip to content

Commit 61bc60f

Browse files
authored
feat(cms): Add upload document endpoint (#262)
* Add publish document endpoint * Add upload document endpoint
1 parent 03d2815 commit 61bc60f

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

backend/endpoints/models/volume_models.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ type (
1616
Image multipart.File
1717
}
1818

19+
// ValidImageUploadRequest is the request model for an handler that uploads an IMAGE to a docker volume
20+
ValidDocumentUploadRequest struct {
21+
DocumentID uuid.UUID `schema:"DocumentID,required"`
22+
Content string `schema:"Content,required"` // TODO: Add check that content is valid JSON
23+
}
24+
1925
// ValidPublishDocumentRequest is the request model for any handler that publishes a document
2026
ValidPublishDocumentRequest struct {
2127
DocumentID uuid.UUID `schema:"DocumentID,required"`

backend/endpoints/registration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func RegisterFilesystemEndpoints(mux *http.ServeMux) {
1010
mux.Handle("/api/filesystem/rename", newHandler("POST", RenameFilesystemEntity, false)) // auth
1111
mux.Handle("/api/filesystem/children", newHandler("GET", GetChildren, false))
1212
mux.Handle("/api/filesystem/upload-image", newHandler("POST", UploadImage, true)) // auth
13+
mux.Handle("/api/filesystem/upload-document", newHandler("POST", UploadDocument, false)) // auth
1314
mux.Handle("/api/filesystem/publish-document", newHandler("POST", PublishDocument, false)) // auth
1415
mux.Handle("/api/filesystem/get/published", newHandler("GET", GetPublishedDocument, false)) // auth
1516
}

backend/endpoints/volume_endpoints.go

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// UploadImage takes an image from a request and uploads it to the published docker volume
1515
func UploadImage(form ValidImageUploadRequest, df DependencyFactory) handlerResponse[NewEntityResponse] {
16-
dockerRepository := df.GetUnpublishedVolumeRepo()
16+
unpublishedVol := df.GetUnpublishedVolumeRepo()
1717
repository := df.GetFilesystemRepo()
1818
log := df.GetLogger()
1919

@@ -28,22 +28,22 @@ func UploadImage(form ValidImageUploadRequest, df DependencyFactory) handlerResp
2828
// Push the new entry into the repository
2929
e, err := repository.CreateEntry(entityToCreate)
3030
if err != nil {
31+
log.Write("failed to create a repository entry")
32+
log.Write(err.Error())
3133
return handlerResponse[NewEntityResponse]{
3234
Status: http.StatusNotAcceptable,
3335
}
3436
}
3537

3638
// Create and get a new entry in docker file system
37-
dockerRepository.AddToVolume(e.EntityID.String())
38-
if dockerFile, err := dockerRepository.GetFromVolume(e.EntityID.String()); err != nil {
39+
unpublishedVol.AddToVolume(e.EntityID.String())
40+
if dockerFile, err := unpublishedVol.GetFromVolume(e.EntityID.String()); err != nil {
3941
return handlerResponse[NewEntityResponse]{Status: http.StatusInternalServerError}
40-
} else {
41-
_, err := io.Copy(dockerFile, form.Image)
42-
if err != nil {
43-
log.Write("failed to write image to docker container")
44-
return handlerResponse[NewEntityResponse]{
45-
Status: http.StatusInternalServerError,
46-
}
42+
} else if _, err := io.Copy(dockerFile, form.Image); err != nil {
43+
log.Write("failed to write image to docker container")
44+
log.Write(err.Error())
45+
return handlerResponse[NewEntityResponse]{
46+
Status: http.StatusInternalServerError,
4747
}
4848
}
4949

@@ -54,27 +54,63 @@ func UploadImage(form ValidImageUploadRequest, df DependencyFactory) handlerResp
5454
}
5555
}
5656

57+
// UploadImage takes an image from a request and uploads it to the published docker volume
58+
func UploadDocument(form ValidDocumentUploadRequest, df DependencyFactory) handlerResponse[NewEntityResponse] {
59+
unpublishedVol := df.GetUnpublishedVolumeRepo()
60+
log := df.GetLogger()
61+
62+
// fetch the target file form the unpublished volume
63+
filename := form.DocumentID.String()
64+
file, err := unpublishedVol.GetFromVolume(filename)
65+
if err != nil {
66+
log.Write(fmt.Sprintf("failed to get file: %s from volume", filename))
67+
log.Write(err.Error())
68+
return handlerResponse[NewEntityResponse]{
69+
Status: http.StatusNotFound,
70+
}
71+
}
72+
73+
bytes, err := file.WriteString(form.Content)
74+
if (bytes == 0 && len(form.Content) != 0) || err != nil {
75+
log.Write("was an error writing to file")
76+
log.Write(err.Error())
77+
return handlerResponse[NewEntityResponse]{
78+
Status: http.StatusInternalServerError,
79+
}
80+
}
81+
82+
return handlerResponse[NewEntityResponse]{
83+
Response: NewEntityResponse{NewID: form.DocumentID},
84+
Status: http.StatusOK,
85+
}
86+
}
87+
5788
// PublishDocument takes in DocumentID and transfers the document from unpublished to published volume if it exists
5889
func PublishDocument(form ValidPublishDocumentRequest, df DependencyFactory) handlerResponse[empty] {
5990
unpublishedVol := df.GetUnpublishedVolumeRepo()
6091
publishedVol := df.GetPublishedVolumeRepo()
92+
log := df.GetLogger()
6193

6294
// fetch the target file form the unpublished volume
6395
filename := form.DocumentID.String()
6496
file, err := unpublishedVol.GetFromVolume(filename)
6597
if err != nil {
98+
log.Write(fmt.Sprintf("failed to get file: %s from volume", filename))
99+
log.Write(err.Error())
66100
return handlerResponse[empty]{
67101
Status: http.StatusNotFound,
68102
}
69103
}
70104

71105
// Copy over to the target volume
72-
if publishedVol.CopyToVolume(file, filename) != nil {
106+
err = publishedVol.CopyToVolume(file, filename)
107+
if err != nil {
108+
log.Write("failed to copy file to published volume")
109+
log.Write(err.Error())
73110
return handlerResponse[empty]{
74111
Status: http.StatusInternalServerError,
75112
}
76113
}
77-
78114
return handlerResponse[empty]{}
79115
}
80116

@@ -86,8 +122,11 @@ func GetPublishedDocument(form ValidGetPublishedDocumentRequest, df DependencyFa
86122
log := df.GetLogger()
87123

88124
// Get file from published volume
89-
file, err := publishedVol.GetFromVolume(form.DocumentID.String())
125+
filename := form.DocumentID.String()
126+
file, err := publishedVol.GetFromVolume(filename)
90127
if err != nil {
128+
log.Write(fmt.Sprintf("failed to get file: %s from volume", filename))
129+
log.Write(err.Error())
91130
return handlerResponse[[]byte]{
92131
Status: http.StatusNotFound,
93132
}
@@ -99,6 +138,7 @@ func GetPublishedDocument(form ValidGetPublishedDocumentRequest, df DependencyFa
99138
bytes, err := buf.ReadFrom(file)
100139
if err != nil || bytes == 0 {
101140
log.Write("failed to read from the requested file")
141+
log.Write(err.Error())
102142
buf.WriteString(emptyFile)
103143
}
104144

0 commit comments

Comments
 (0)