@@ -13,7 +13,7 @@ import (
13
13
14
14
// UploadImage takes an image from a request and uploads it to the published docker volume
15
15
func UploadImage (form ValidImageUploadRequest , df DependencyFactory ) handlerResponse [NewEntityResponse ] {
16
- dockerRepository := df .GetUnpublishedVolumeRepo ()
16
+ unpublishedVol := df .GetUnpublishedVolumeRepo ()
17
17
repository := df .GetFilesystemRepo ()
18
18
log := df .GetLogger ()
19
19
@@ -28,22 +28,22 @@ func UploadImage(form ValidImageUploadRequest, df DependencyFactory) handlerResp
28
28
// Push the new entry into the repository
29
29
e , err := repository .CreateEntry (entityToCreate )
30
30
if err != nil {
31
+ log .Write ("failed to create a repository entry" )
32
+ log .Write (err .Error ())
31
33
return handlerResponse [NewEntityResponse ]{
32
34
Status : http .StatusNotAcceptable ,
33
35
}
34
36
}
35
37
36
38
// 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 {
39
41
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 ,
47
47
}
48
48
}
49
49
@@ -54,27 +54,63 @@ func UploadImage(form ValidImageUploadRequest, df DependencyFactory) handlerResp
54
54
}
55
55
}
56
56
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
+
57
88
// PublishDocument takes in DocumentID and transfers the document from unpublished to published volume if it exists
58
89
func PublishDocument (form ValidPublishDocumentRequest , df DependencyFactory ) handlerResponse [empty ] {
59
90
unpublishedVol := df .GetUnpublishedVolumeRepo ()
60
91
publishedVol := df .GetPublishedVolumeRepo ()
92
+ log := df .GetLogger ()
61
93
62
94
// fetch the target file form the unpublished volume
63
95
filename := form .DocumentID .String ()
64
96
file , err := unpublishedVol .GetFromVolume (filename )
65
97
if err != nil {
98
+ log .Write (fmt .Sprintf ("failed to get file: %s from volume" , filename ))
99
+ log .Write (err .Error ())
66
100
return handlerResponse [empty ]{
67
101
Status : http .StatusNotFound ,
68
102
}
69
103
}
70
104
71
105
// 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 ())
73
110
return handlerResponse [empty ]{
74
111
Status : http .StatusInternalServerError ,
75
112
}
76
113
}
77
-
78
114
return handlerResponse [empty ]{}
79
115
}
80
116
@@ -86,8 +122,11 @@ func GetPublishedDocument(form ValidGetPublishedDocumentRequest, df DependencyFa
86
122
log := df .GetLogger ()
87
123
88
124
// Get file from published volume
89
- file , err := publishedVol .GetFromVolume (form .DocumentID .String ())
125
+ filename := form .DocumentID .String ()
126
+ file , err := publishedVol .GetFromVolume (filename )
90
127
if err != nil {
128
+ log .Write (fmt .Sprintf ("failed to get file: %s from volume" , filename ))
129
+ log .Write (err .Error ())
91
130
return handlerResponse [[]byte ]{
92
131
Status : http .StatusNotFound ,
93
132
}
@@ -99,6 +138,7 @@ func GetPublishedDocument(form ValidGetPublishedDocumentRequest, df DependencyFa
99
138
bytes , err := buf .ReadFrom (file )
100
139
if err != nil || bytes == 0 {
101
140
log .Write ("failed to read from the requested file" )
141
+ log .Write (err .Error ())
102
142
buf .WriteString (emptyFile )
103
143
}
104
144
0 commit comments