You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransferHandler.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lfs.server;
  11. import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
  12. import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
  13. import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
  14. import static org.eclipse.jgit.lfs.lib.Constants.VERIFY;
  15. import java.io.IOException;
  16. import java.text.MessageFormat;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import org.eclipse.jgit.lfs.lib.LongObjectId;
  21. import org.eclipse.jgit.lfs.server.Response.Action;
  22. import org.eclipse.jgit.lfs.server.Response.Body;
  23. import org.eclipse.jgit.lfs.server.internal.LfsServerText;
  24. abstract class TransferHandler {
  25. static TransferHandler forOperation(String operation,
  26. LargeFileRepository repository, List<LfsObject> objects) {
  27. switch (operation) {
  28. case UPLOAD:
  29. return new Upload(repository, objects);
  30. case DOWNLOAD:
  31. return new Download(repository, objects);
  32. case VERIFY:
  33. default:
  34. throw new UnsupportedOperationException(MessageFormat.format(
  35. LfsServerText.get().unsupportedOperation, operation));
  36. }
  37. }
  38. private static class Upload extends TransferHandler {
  39. Upload(LargeFileRepository repository,
  40. List<LfsObject> objects) {
  41. super(repository, objects);
  42. }
  43. @Override
  44. Body process() throws IOException {
  45. Response.Body body = new Response.Body();
  46. if (!objects.isEmpty()) {
  47. body.objects = new ArrayList<>();
  48. for (LfsObject o : objects) {
  49. addObjectInfo(body, o);
  50. }
  51. }
  52. return body;
  53. }
  54. private void addObjectInfo(Response.Body body, LfsObject o)
  55. throws IOException {
  56. Response.ObjectInfo info = new Response.ObjectInfo();
  57. body.objects.add(info);
  58. info.oid = o.oid;
  59. info.size = o.size;
  60. LongObjectId oid = LongObjectId.fromString(o.oid);
  61. if (repository.getSize(oid) == -1) {
  62. info.actions = new HashMap<>();
  63. info.actions.put(UPLOAD,
  64. repository.getUploadAction(oid, o.size));
  65. Action verify = repository.getVerifyAction(oid);
  66. if (verify != null) {
  67. info.actions.put(VERIFY, verify);
  68. }
  69. }
  70. }
  71. }
  72. private static class Download extends TransferHandler {
  73. Download(LargeFileRepository repository,
  74. List<LfsObject> objects) {
  75. super(repository, objects);
  76. }
  77. @Override
  78. Body process() throws IOException {
  79. Response.Body body = new Response.Body();
  80. if (!objects.isEmpty()) {
  81. body.objects = new ArrayList<>();
  82. for (LfsObject o : objects) {
  83. addObjectInfo(body, o);
  84. }
  85. }
  86. return body;
  87. }
  88. private void addObjectInfo(Response.Body body, LfsObject o)
  89. throws IOException {
  90. Response.ObjectInfo info = new Response.ObjectInfo();
  91. body.objects.add(info);
  92. info.oid = o.oid;
  93. info.size = o.size;
  94. LongObjectId oid = LongObjectId.fromString(o.oid);
  95. if (repository.getSize(oid) >= 0) {
  96. info.actions = new HashMap<>();
  97. info.actions.put(DOWNLOAD,
  98. repository.getDownloadAction(oid));
  99. } else {
  100. info.error = new Response.Error();
  101. info.error.code = SC_NOT_FOUND;
  102. info.error.message = MessageFormat.format(
  103. LfsServerText.get().objectNotFound,
  104. oid.getName());
  105. }
  106. }
  107. }
  108. final LargeFileRepository repository;
  109. final List<LfsObject> objects;
  110. TransferHandler(LargeFileRepository repository,
  111. List<LfsObject> objects) {
  112. this.repository = repository;
  113. this.objects = objects;
  114. }
  115. abstract Response.Body process() throws IOException;
  116. }