use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class CompressionUtils method tar.
/**
* Compresses/archives the contents of the tree at the (optionally)
* specified revision and the (optionally) specified basepath to the
* supplied outputstream.
*
* @param algorithm
* compression algorithm for tar (optional)
* @param repository
* @param basePath
* if unspecified, entire repository is assumed.
* @param objectId
* if unspecified, HEAD is assumed.
* @param os
* @return true if repository was successfully zipped to supplied output
* stream
*/
private static boolean tar(String algorithm, Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) {
RevCommit commit = JGitUtils.getCommit(repository, objectId);
if (commit == null) {
return false;
}
OutputStream cos = os;
if (!StringUtils.isEmpty(algorithm)) {
try {
cos = new CompressorStreamFactory().createCompressorOutputStream(algorithm, os);
} catch (CompressorException e1) {
error(e1, repository, "{0} failed to open {1} stream", algorithm);
}
}
boolean success = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
tos.setAddPaxHeadersForNonAsciiNames(true);
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
if (!StringUtils.isEmpty(basePath)) {
PathFilter f = PathFilter.create(basePath);
tw.setFilter(f);
}
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
long modified = commit.getAuthorIdent().getWhen().getTime();
while (tw.next()) {
FileMode mode = tw.getFileMode(0);
if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
continue;
}
tw.getObjectId(id, 0);
ObjectLoader loader = repository.open(id);
if (FileMode.SYMLINK == mode) {
TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(), TarArchiveEntry.LF_SYMLINK);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
loader.copyTo(bos);
entry.setLinkName(bos.toString());
entry.setModTime(modified);
tos.putArchiveEntry(entry);
tos.closeArchiveEntry();
} else {
TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());
entry.setMode(mode.getBits());
entry.setModTime(modified);
FilestoreModel filestoreItem = null;
if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) {
filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id));
}
final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize();
entry.setSize(size);
tos.putArchiveEntry(entry);
if (filestoreItem == null) {
//Copy repository stored file
loader.copyTo(tos);
} else {
//Copy filestore file
try (FileInputStream streamIn = new FileInputStream(filestoreManager.getStoragePath(filestoreItem.oid))) {
IOUtils.copyLarge(streamIn, tos);
} catch (Throwable e) {
LOGGER.error(MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e);
//Handle as per other errors
throw e;
}
}
tos.closeArchiveEntry();
}
}
tos.finish();
tos.close();
cos.close();
success = true;
} catch (IOException e) {
error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName());
} finally {
tw.close();
rw.dispose();
}
return success;
}
use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class JGitUtils method getPathModel.
/**
* Returns a path model by path string
*
* @param repo
* @param path
* @param filter
* @param commit
* @return a path model of the specified object
*/
private static PathModel getPathModel(Repository repo, String path, String filter, RevCommit commit) throws IOException {
long size = 0;
FilestoreModel filestoreItem = null;
TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree());
String pathString = path;
if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
pathString = PathUtils.getLastPathComponent(pathString);
size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
if (isPossibleFilestoreItem(size)) {
filestoreItem = getFilestoreItem(tw.getObjectReader().open(tw.getObjectId(0)));
}
} else if (tw.isSubtree()) {
// do not display dirs that are behind in the path
if (!Strings.isNullOrEmpty(filter)) {
pathString = path.replaceFirst(filter + "/", "");
}
// remove the last slash from path in displayed link
if (pathString != null && pathString.charAt(pathString.length() - 1) == '/') {
pathString = pathString.substring(0, pathString.length() - 1);
}
}
return new PathModel(pathString, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), tw.getObjectId(0).getName(), commit.getName());
}
use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class FilestoreServlet method doGet.
/**
* Handles a download
* Treated as hypermedia request if accept header contains Git-LFS MIME
* otherwise treated as a download of the blob
* @param request
* @param response
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UrlInfo info = getInfoFromRequest(request);
if (info == null || info.oid == null) {
sendError(response, HttpServletResponse.SC_NOT_FOUND);
return;
}
UserModel user = getUserOrAnonymous(request);
FilestoreModel model = gitblit.getObject(info.oid, user, info.repository);
long size = FilestoreManager.UNDEFINED_SIZE;
boolean isMetaRequest = AccessRestrictionFilter.hasContentInRequestHeader(request, "Accept", GIT_LFS_META_MIME);
FilestoreModel.Status status = Status.Unavailable;
if (model != null) {
size = model.getSize();
status = model.getStatus();
}
if (!isMetaRequest) {
status = gitblit.downloadBlob(info.oid, user, info.repository, response.getOutputStream());
logger.info(MessageFormat.format("FILESTORE-AUDIT {0}:{4} {1} {2}@{3}", "GET", info.oid, user.getName(), info.repository.name, status.toString()));
}
if (status == Status.Error_Unexpected_Stream_End) {
return;
}
IGitLFS.Response responseObject = getResponseForDownload(info.baseUrl, info.oid, size, user.getName(), info.repository.name, status);
if (responseObject.error == null) {
response.setStatus(responseObject.successCode);
if (isMetaRequest) {
serialize(response, responseObject);
}
} else {
response.setStatus(responseObject.error.code);
if (isMetaRequest) {
serialize(response, responseObject.error);
}
}
}
use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class FilestoreManager method start.
@Override
public IManager start() {
// Try to load any existing metadata
File dir = getStorageFolder();
dir.mkdirs();
File metadata = new File(dir, METAFILE);
if (metadata.exists()) {
Collection<FilestoreModel> items = null;
Gson gson = gson();
try (FileReader file = new FileReader(metadata)) {
items = gson.fromJson(file, METAFILE_TYPE);
file.close();
} catch (IOException e) {
e.printStackTrace();
}
for (Iterator<FilestoreModel> itr = items.iterator(); itr.hasNext(); ) {
FilestoreModel model = itr.next();
fileCache.put(model.oid, model);
}
logger.info("Loaded {} items from filestore metadata file", fileCache.size());
} else {
logger.info("No filestore metadata file found");
}
return this;
}
use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class FilestoreManager method addObject.
@Override
public FilestoreModel.Status addObject(String oid, long size, UserModel user, RepositoryModel repo) {
//Handle access control
if (!user.canPush(repo)) {
if (user == UserModel.ANONYMOUS) {
return Status.AuthenticationRequired;
} else {
return Status.Error_Unauthorized;
}
}
//Handle object details
if (!isValidOid(oid)) {
return Status.Error_Invalid_Oid;
}
if (fileCache.containsKey(oid)) {
FilestoreModel item = fileCache.get(oid);
if (!item.isInErrorState() && (size != UNDEFINED_SIZE) && (item.getSize() != size)) {
return Status.Error_Size_Mismatch;
}
item.addRepository(repo.name);
if (item.isInErrorState()) {
item.reset(user, size);
}
} else {
if (size < 0) {
return Status.Error_Invalid_Size;
}
if ((getMaxUploadSize() != UNDEFINED_SIZE) && (size > getMaxUploadSize())) {
return Status.Error_Exceeds_Size_Limit;
}
FilestoreModel model = new FilestoreModel(oid, size, user, repo.name);
fileCache.put(oid, model);
saveFilestoreModel(model);
}
return fileCache.get(oid).getStatus();
}
Aggregations