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 package org.eclipse.jgit.lfs.server;
45
46 import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
47 import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
48 import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
49 import static org.eclipse.jgit.lfs.lib.Constants.VERIFY;
50
51 import java.io.IOException;
52 import java.text.MessageFormat;
53 import java.util.ArrayList;
54 import java.util.HashMap;
55 import java.util.List;
56
57 import org.eclipse.jgit.lfs.lib.LongObjectId;
58 import org.eclipse.jgit.lfs.server.Response.Action;
59 import org.eclipse.jgit.lfs.server.Response.Body;
60 import org.eclipse.jgit.lfs.server.internal.LfsServerText;
61
62 abstract class TransferHandler {
63
64 static TransferHandler forOperation(String operation,
65 LargeFileRepository repository, List<LfsObject> objects) {
66 switch (operation) {
67 case UPLOAD:
68 return new Upload(repository, objects);
69 case DOWNLOAD:
70 return new Download(repository, objects);
71 case VERIFY:
72 default:
73 throw new UnsupportedOperationException(MessageFormat.format(
74 LfsServerText.get().unsupportedOperation, operation));
75 }
76 }
77
78 private static class Upload extends TransferHandler {
79 Upload(LargeFileRepository repository,
80 List<LfsObject> objects) {
81 super(repository, objects);
82 }
83
84 @Override
85 Body process() throws IOException {
86 Response.Body body = new Response.Body();
87 if (objects.size() > 0) {
88 body.objects = new ArrayList<>();
89 for (LfsObject o : objects) {
90 addObjectInfo(body, o);
91 }
92 }
93 return body;
94 }
95
96 private void addObjectInfo(Response.Body body, LfsObject o)
97 throws IOException {
98 Response.ObjectInfo info = new Response.ObjectInfo();
99 body.objects.add(info);
100 info.oid = o.oid;
101 info.size = o.size;
102
103 LongObjectId oid = LongObjectId.fromString(o.oid);
104 if (repository.getSize(oid) == -1) {
105 info.actions = new HashMap<>();
106 info.actions.put(UPLOAD,
107 repository.getUploadAction(oid, o.size));
108 Action verify = repository.getVerifyAction(oid);
109 if (verify != null) {
110 info.actions.put(VERIFY, verify);
111 }
112 }
113 }
114 }
115
116 private static class Download extends TransferHandler {
117 Download(LargeFileRepository repository,
118 List<LfsObject> objects) {
119 super(repository, objects);
120 }
121
122 @Override
123 Body process() throws IOException {
124 Response.Body body = new Response.Body();
125 if (objects.size() > 0) {
126 body.objects = new ArrayList<>();
127 for (LfsObject o : objects) {
128 addObjectInfo(body, o);
129 }
130 }
131 return body;
132 }
133
134 private void addObjectInfo(Response.Body body, LfsObject o)
135 throws IOException {
136 Response.ObjectInfo info = new Response.ObjectInfo();
137 body.objects.add(info);
138 info.oid = o.oid;
139 info.size = o.size;
140
141 LongObjectId oid = LongObjectId.fromString(o.oid);
142 if (repository.getSize(oid) >= 0) {
143 info.actions = new HashMap<>();
144 info.actions.put(DOWNLOAD,
145 repository.getDownloadAction(oid));
146 } else {
147 info.error = new Response.Error();
148 info.error.code = SC_NOT_FOUND;
149 info.error.message = MessageFormat.format(
150 LfsServerText.get().objectNotFound,
151 oid.getName());
152 }
153 }
154 }
155
156 final LargeFileRepository repository;
157
158 final List<LfsObject> objects;
159
160 TransferHandler(LargeFileRepository repository,
161 List<LfsObject> objects) {
162 this.repository = repository;
163 this.objects = objects;
164 }
165
166 abstract Response.Body process() throws IOException;
167 }