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.

FilestoreModel.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2015 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.models;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.NoSuchElementException;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. import com.gitblit.Constants;
  25. /**
  26. * A FilestoreModel represents a file stored outside a repository but referenced by the repository using a unique objectID
  27. *
  28. * @author Paul Martin
  29. *
  30. */
  31. public class FilestoreModel implements Serializable, Comparable<FilestoreModel> {
  32. private static final long serialVersionUID = 1L;
  33. private static final String metaRegexText = new StringBuilder()
  34. .append("version\\shttps://git-lfs.github.com/spec/v1\\s+")
  35. .append("oid\\ssha256:(" + Constants.REGEX_SHA256 + ")\\s+")
  36. .append("size\\s([0-9]+)")
  37. .toString();
  38. private static final Pattern metaRegex = Pattern.compile(metaRegexText);
  39. private static final int metaRegexIndexSHA = 1;
  40. private static final int metaRegexIndexSize = 2;
  41. public final String oid;
  42. private Long size;
  43. private Status status;
  44. //Audit
  45. private String stateChangedBy;
  46. private Date stateChangedOn;
  47. //Access Control
  48. private List<String> repositories;
  49. public FilestoreModel(String id, long definedSize) {
  50. oid = id;
  51. size = definedSize;
  52. status = Status.ReferenceOnly;
  53. }
  54. public FilestoreModel(String id, long expectedSize, UserModel user, String repo) {
  55. oid = id;
  56. size = expectedSize;
  57. status = Status.Upload_Pending;
  58. stateChangedBy = user.getName();
  59. stateChangedOn = new Date();
  60. repositories = new ArrayList<String>();
  61. repositories.add(repo);
  62. }
  63. /*
  64. * Attempts to create a FilestoreModel from the given meta string
  65. *
  66. * @return A valid FilestoreModel if successful, otherwise null
  67. */
  68. public static FilestoreModel fromMetaString(String meta) {
  69. Matcher m = metaRegex.matcher(meta);
  70. if (m.find()) {
  71. try
  72. {
  73. final Long size = Long.parseLong(m.group(metaRegexIndexSize));
  74. final String sha = m.group(metaRegexIndexSHA);
  75. return new FilestoreModel(sha, size);
  76. } catch (Exception e) {
  77. //Fail silent - it is not a valid filestore item
  78. }
  79. }
  80. return null;
  81. }
  82. public synchronized long getSize() {
  83. return size;
  84. }
  85. public synchronized Status getStatus() {
  86. return status;
  87. }
  88. public synchronized String getChangedBy() {
  89. return stateChangedBy;
  90. }
  91. public synchronized Date getChangedOn() {
  92. return stateChangedOn;
  93. }
  94. public synchronized void setStatus(Status status, UserModel user) {
  95. this.status = status;
  96. stateChangedBy = user.getName();
  97. stateChangedOn = new Date();
  98. }
  99. public synchronized void reset(UserModel user, long size) {
  100. status = Status.Upload_Pending;
  101. stateChangedBy = user.getName();
  102. stateChangedOn = new Date();
  103. this.size = size;
  104. }
  105. /*
  106. * Handles possible race condition with concurrent connections
  107. * @return true if action can proceed, false otherwise
  108. */
  109. public synchronized boolean actionUpload(UserModel user) {
  110. if (status == Status.Upload_Pending) {
  111. status = Status.Upload_In_Progress;
  112. stateChangedBy = user.getName();
  113. stateChangedOn = new Date();
  114. return true;
  115. }
  116. return false;
  117. }
  118. public synchronized boolean isInErrorState() {
  119. return (this.status.value < 0);
  120. }
  121. public synchronized void addRepository(String repo) {
  122. if (status != Status.ReferenceOnly) {
  123. if (!repositories.contains(repo)) {
  124. repositories.add(repo);
  125. }
  126. }
  127. }
  128. public synchronized void removeRepository(String repo) {
  129. if (status != Status.ReferenceOnly) {
  130. repositories.remove(repo);
  131. }
  132. }
  133. public synchronized boolean isInRepositoryList(List<String> repoList) {
  134. if (status != Status.ReferenceOnly) {
  135. for (String name : repositories) {
  136. if (repoList.contains(name)) {
  137. return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. public static enum Status {
  144. ReferenceOnly(-42),
  145. Deleted(-30),
  146. AuthenticationRequired(-20),
  147. Error_Unknown(-8),
  148. Error_Unexpected_Stream_End(-7),
  149. Error_Invalid_Oid(-6),
  150. Error_Invalid_Size(-5),
  151. Error_Hash_Mismatch(-4),
  152. Error_Size_Mismatch(-3),
  153. Error_Exceeds_Size_Limit(-2),
  154. Error_Unauthorized(-1),
  155. //Negative values provide additional information and may be treated as 0 when not required
  156. Unavailable(0),
  157. Upload_Pending(1),
  158. Upload_In_Progress(2),
  159. Available(3);
  160. final int value;
  161. Status(int value) {
  162. this.value = value;
  163. }
  164. public int getValue() {
  165. return value;
  166. }
  167. @Override
  168. public String toString() {
  169. return name().toLowerCase().replace('_', ' ');
  170. }
  171. public static Status fromState(int state) {
  172. for (Status s : values()) {
  173. if (s.getValue() == state) {
  174. return s;
  175. }
  176. }
  177. throw new NoSuchElementException(String.valueOf(state));
  178. }
  179. }
  180. @Override
  181. public int compareTo(FilestoreModel o) {
  182. return this.oid.compareTo(o.oid);
  183. }
  184. }