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