|
5 | 5 | import static io.onedev.server.model.Project.decodeFullRepoNameAsPath; |
6 | 6 | import static io.onedev.server.util.CollectionUtils.newHashMap; |
7 | 7 | import static io.onedev.server.util.IOUtils.BUFFER_SIZE; |
8 | | -import static io.onedev.server.workspace.WorkspaceService.GIT_PREFIX; |
9 | 8 | import static javax.servlet.http.HttpServletResponse.SC_CONFLICT; |
10 | 9 | import static javax.servlet.http.HttpServletResponse.SC_CREATED; |
11 | 10 | import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; |
|
16 | 15 | import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; |
17 | 16 | import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION; |
18 | 17 | import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM; |
19 | | -import static org.apache.commons.lang3.StringUtils.strip; |
20 | 18 | import static org.apache.commons.lang3.StringUtils.substringBeforeLast; |
21 | 19 | import static org.apache.tika.mime.MimeTypes.OCTET_STREAM; |
22 | 20 | import static org.glassfish.jersey.client.ClientProperties.REQUEST_ENTITY_PROCESSING; |
|
69 | 67 | import io.onedev.server.cluster.ClusterService; |
70 | 68 | import io.onedev.server.model.GitLfsLock; |
71 | 69 | import io.onedev.server.model.Project; |
72 | | -import io.onedev.server.model.Workspace; |
73 | 70 | import io.onedev.server.persistence.SessionService; |
74 | 71 | import io.onedev.server.persistence.dao.EntityCriteria; |
75 | 72 | import io.onedev.server.security.CodePullAuthorizationSource; |
@@ -167,46 +164,6 @@ private boolean canAccessProject(HttpServletRequest request, Project project) { |
167 | 164 | } |
168 | 165 | } |
169 | 166 |
|
170 | | - private boolean isWorkspacePath(String pathInfo) { |
171 | | - return pathInfo.contains(GIT_PREFIX); |
172 | | - } |
173 | | - |
174 | | - @Nullable |
175 | | - private long[] parseWorkspaceInfo(String pathInfo) { |
176 | | - int idx = pathInfo.indexOf(GIT_PREFIX); |
177 | | - if (idx < 0) |
178 | | - return null; |
179 | | - String projectPath = strip(pathInfo.substring(0, idx), "/"); |
180 | | - String afterWorkspaces = pathInfo.substring(idx + GIT_PREFIX.length()); |
181 | | - String numberStr; |
182 | | - int dotGitIdx = afterWorkspaces.indexOf(".git/"); |
183 | | - if (dotGitIdx >= 0) { |
184 | | - numberStr = afterWorkspaces.substring(0, dotGitIdx); |
185 | | - } else { |
186 | | - int slashIdx = afterWorkspaces.indexOf('/'); |
187 | | - numberStr = slashIdx >= 0 ? afterWorkspaces.substring(0, slashIdx) : afterWorkspaces; |
188 | | - } |
189 | | - var facade = projectService.findFacadeByPath(projectPath); |
190 | | - if (facade == null) |
191 | | - throw new ExplicitException("Project not found: " + projectPath); |
192 | | - long workspaceNumber = Long.parseLong(numberStr); |
193 | | - return new long[]{facade.getId(), workspaceNumber}; |
194 | | - } |
195 | | - |
196 | | - private File getWorkspaceLfsFile(long projectId, long workspaceNumber, String objectId) { |
197 | | - File workDir = Workspace.getWorkDir(projectId, workspaceNumber); |
198 | | - return new File(workDir, ".git/lfs/objects/" |
199 | | - + objectId.substring(0, 2) + "/" |
200 | | - + objectId.substring(2, 4) + "/" + objectId); |
201 | | - } |
202 | | - |
203 | | - private String getWorkspaceObjectUrl(long projectId, long workspaceNumber, String objectId) { |
204 | | - var serverUrl = clusterService.getServerUrl(clusterService.getLocalServerAddress()); |
205 | | - var projectPath = projectService.findFacadeById(projectId).getPath(); |
206 | | - return String.format("%s/%s/%s%d.git/lfs/objects/%s?lfs-objects=true", |
207 | | - serverUrl, projectPath, GIT_PREFIX, workspaceNumber, objectId); |
208 | | - } |
209 | | - |
210 | 167 | private String getObjectUrl(HttpServletRequest request, String projectPath, |
211 | 168 | String objectId, boolean clusterAccess) { |
212 | 169 | var serverUrl = clusterAccess ? clusterService.getServerUrl(clusterService.getLocalServerAddress()) : settingService.getSystemSetting().getServerUrl(); |
@@ -241,32 +198,6 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha |
241 | 198 | boolean clusterAccess = SecurityUtils.isSystem(); |
242 | 199 |
|
243 | 200 | if ("true".equals(httpRequest.getParameter("lfs-objects"))) { |
244 | | - if (isWorkspacePath(pathInfo)) { |
245 | | - if (!clusterAccess) { |
246 | | - sendBatchError(httpResponse, SC_UNAUTHORIZED, |
247 | | - "Cluster credential required to access workspace LFS"); |
248 | | - return; |
249 | | - } |
250 | | - if (!httpRequest.getMethod().equals("GET")) { |
251 | | - sendBatchError(httpResponse, SC_FORBIDDEN, |
252 | | - "Upload to workspace LFS is not supported"); |
253 | | - return; |
254 | | - } |
255 | | - long[] workspaceInfo = parseWorkspaceInfo(pathInfo); |
256 | | - String objectId = StringUtils.substringAfterLast(pathInfo, "/"); |
257 | | - File lfsFile = getWorkspaceLfsFile(workspaceInfo[0], workspaceInfo[1], objectId); |
258 | | - if (lfsFile.exists()) { |
259 | | - httpResponse.setContentType(OCTET_STREAM); |
260 | | - try (InputStream is = new FileInputStream(lfsFile); |
261 | | - OutputStream os = httpResponse.getOutputStream()) { |
262 | | - IOUtils.copy(is, os, BUFFER_SIZE); |
263 | | - } |
264 | | - } else { |
265 | | - httpResponse.setStatus(SC_NOT_FOUND); |
266 | | - } |
267 | | - return; |
268 | | - } |
269 | | - |
270 | 201 | String projectPath = getProjectPath(pathInfo); |
271 | 202 | String objectId = StringUtils.substringAfterLast(pathInfo, "/"); |
272 | 203 |
|
@@ -394,23 +325,7 @@ else if (canWriteCode(httpRequest, project)) |
394 | 325 | && httpRequest.getContentType().startsWith(CONTENT_TYPE) |
395 | 326 | || httpRequest.getHeader("Accept") != null |
396 | 327 | && httpRequest.getHeader("Accept").startsWith(CONTENT_TYPE)) { |
397 | | - if (isWorkspacePath(pathInfo)) { |
398 | | - if (!clusterAccess) { |
399 | | - sendBatchError(httpResponse, SC_UNAUTHORIZED, |
400 | | - "Cluster credential required to access workspace LFS"); |
401 | | - return; |
402 | | - } |
403 | | - httpResponse.setContentType(CONTENT_TYPE); |
404 | | - if (pathInfo.endsWith("/batch")) { |
405 | | - long[] workspaceInfo = parseWorkspaceInfo(pathInfo); |
406 | | - processWorkspaceBatch(httpRequest, httpResponse, workspaceInfo[0], workspaceInfo[1]); |
407 | | - } else { |
408 | | - sendBatchError(httpResponse, SC_NOT_IMPLEMENTED, |
409 | | - "Locks are not supported for workspace LFS"); |
410 | | - } |
411 | | - return; |
412 | | - } |
413 | | - String projectPath = getProjectPath(pathInfo); |
| 328 | + String projectPath = getProjectPath(pathInfo); |
414 | 329 | if (clusterAccess) { |
415 | 330 | ProjectFacade project = projectService.findFacadeByPath(projectPath); |
416 | 331 | if (project == null) { |
@@ -677,46 +592,6 @@ private void processBatch(HttpServletRequest httpRequest, HttpServletResponse ht |
677 | 592 | writeTo(httpResponse, batchResponse); |
678 | 593 | } |
679 | 594 | } |
680 | | - |
681 | | - private void processWorkspaceBatch(HttpServletRequest httpRequest, HttpServletResponse httpResponse, |
682 | | - long projectId, long workspaceNumber) { |
683 | | - JsonNode batchRequestNode = parseAndValidateBatchRequest(httpRequest, httpResponse); |
684 | | - if (batchRequestNode == null) |
685 | | - return; |
686 | | - |
687 | | - boolean upload = batchRequestNode.get("operation").asText().equals("upload"); |
688 | | - if (upload) { |
689 | | - sendBatchError(httpResponse, SC_FORBIDDEN, |
690 | | - "Upload to workspace LFS is not supported"); |
691 | | - return; |
692 | | - } |
693 | | - |
694 | | - List<Map<String, Object>> objectsResponse = new ArrayList<>(); |
695 | | - for (JsonNode objectNode : batchRequestNode.get("objects")) { |
696 | | - String objectId = objectNode.get("oid").asText(); |
697 | | - long objectSize = objectNode.get("size").asLong(); |
698 | | - Map<String, Object> objectResponse = new HashMap<>(); |
699 | | - objectResponse.put("oid", objectId); |
700 | | - objectResponse.put("size", objectSize); |
701 | | - File lfsFile = getWorkspaceLfsFile(projectId, workspaceNumber, objectId); |
702 | | - if (lfsFile.exists()) { |
703 | | - Map<Object, Object> actionResponse = newHashMap( |
704 | | - "href", getWorkspaceObjectUrl(projectId, workspaceNumber, objectId)); |
705 | | - actionResponse.put("header", newHashMap( |
706 | | - "Authorization", BEARER + " " + clusterService.getCredential())); |
707 | | - objectResponse.put("actions", newHashMap("download", actionResponse)); |
708 | | - } else { |
709 | | - objectResponse.put("error", newHashMap( |
710 | | - "code", SC_NOT_FOUND, |
711 | | - "message", "Object not found")); |
712 | | - } |
713 | | - objectsResponse.add(objectResponse); |
714 | | - } |
715 | | - |
716 | | - Map<String, Object> batchResponse = new HashMap<>(); |
717 | | - batchResponse.put("objects", objectsResponse); |
718 | | - writeTo(httpResponse, batchResponse); |
719 | | - } |
720 | 595 |
|
721 | 596 | private void sendAuthorizationError(HttpServletResponse response) { |
722 | 597 | if (SecurityUtils.getUser() != null) { |
|
0 commit comments