private static char[] toHexCharArray(int b) { final char[] dst = new char[2]; formatHexChar(dst, 0, b); return dst; }
/** {@inheritDoc} */ @Override public Response.Action getDownloadAction(AnyLongObjectId id) { return getAction(id); }
ReadableByteChannel getReadChannel(AnyLongObjectId id) throws IOException { return FileChannel.open(getPath(id), StandardOpenOption.READ); }
/** * @param repository * the repository storing large objects * @param context * the servlet asynchronous context * @param response * the servlet response * @param id * id of the object to be downloaded * @throws IOException */ public ObjectDownloadListener(FileLfsRepository repository, AsyncContext context, HttpServletResponse response, AnyLongObjectId id) throws IOException { this.context = context; this.response = response; this.in = repository.getReadChannel(id); this.out = response.getOutputStream(); this.outChannel = Channels.newChannel(out); response.addHeader(HttpSupport.HDR_CONTENT_LENGTH, String.valueOf(repository.getSize(id))); response.setContentType(Constants.HDR_APPLICATION_OCTET_STREAM); }
/** * Get the path where the given object is stored * * @param id * id of a large object * @return path the object's storage path */ protected Path getPath(AnyLongObjectId id) { StringBuilder s = new StringBuilder( Constants.LONG_OBJECT_ID_STRING_LENGTH + 6); s.append(toHexCharArray(id.getFirstByte())).append('/'); s.append(toHexCharArray(id.getSecondByte())).append('/'); s.append(id.name()); return dir.resolve(s.toString()); }
/** * {@inheritDoc} * * Handle object downloads */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { AnyLongObjectId obj = getObjectToTransfer(req, rsp); if (obj != null) { if (repository.getSize(obj) == -1) { sendError(rsp, HttpStatus.SC_NOT_FOUND, MessageFormat .format(LfsServerText.get().objectNotFound, obj.getName())); return; } AsyncContext context = req.startAsync(); context.setTimeout(timeout); rsp.getOutputStream() .setWriteListener(new ObjectDownloadListener(repository, context, rsp, obj)); } }
/** * @param repository * the repository storing large objects * @param context * @param request * @param response * @param id * @throws FileNotFoundException * @throws IOException */ public ObjectUploadListener(FileLfsRepository repository, AsyncContext context, HttpServletRequest request, HttpServletResponse response, AnyLongObjectId id) throws FileNotFoundException, IOException { this.context = context; this.response = response; this.in = request.getInputStream(); this.inChannel = Channels.newChannel(in); this.out = repository.getOutputStream(id); this.channel = Channels.newChannel(out); response.setContentType(Constants.CONTENT_TYPE_GIT_LFS_JSON); }
/** * <p>Constructor for ObjectDownloadListener.</p> * * @param repository * the repository storing large objects * @param context * the servlet asynchronous context * @param response * the servlet response * @param id * id of the object to be downloaded * @throws java.io.IOException */ public ObjectDownloadListener(FileLfsRepository repository, AsyncContext context, HttpServletResponse response, AnyLongObjectId id) throws IOException { this.context = context; this.response = response; this.in = repository.getReadChannel(id); this.out = response.getOutputStream(); this.outChannel = Channels.newChannel(out); response.addHeader(HttpSupport.HDR_CONTENT_LENGTH, String.valueOf(repository.getSize(id))); response.setContentType(Constants.HDR_APPLICATION_OCTET_STREAM); }
/** * Get the path where the given object is stored * * @param id * id of a large object * @return path the object's storage path */ protected Path getPath(AnyLongObjectId id) { StringBuilder s = new StringBuilder( Constants.LONG_OBJECT_ID_STRING_LENGTH + 6); s.append(toHexCharArray(id.getFirstByte())).append('/'); s.append(toHexCharArray(id.getSecondByte())).append('/'); s.append(id.name()); return dir.resolve(s.toString()); }
/** * Handles object downloads * * @param req * servlet request * @param rsp * servlet response * @throws ServletException * if a servlet-specific error occurs * @throws IOException * if an I/O error occurs */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { AnyLongObjectId obj = getObjectToTransfer(req, rsp); if (obj != null) { if (repository.getSize(obj) == -1) { sendError(rsp, HttpStatus.SC_NOT_FOUND, MessageFormat .format(LfsServerText.get().objectNotFound, obj.getName())); return; } AsyncContext context = req.startAsync(); context.setTimeout(timeout); rsp.getOutputStream() .setWriteListener(new ObjectDownloadListener(repository, context, rsp, obj)); } }
/** * Constructor for ObjectUploadListener. * * @param repository * the repository storing large objects * @param context * a {@link javax.servlet.AsyncContext} object. * @param request * a {@link javax.servlet.http.HttpServletRequest} object. * @param response * a {@link javax.servlet.http.HttpServletResponse} object. * @param id * a {@link org.eclipse.jgit.lfs.lib.AnyLongObjectId} object. * @throws java.io.FileNotFoundException * @throws java.io.IOException */ public ObjectUploadListener(FileLfsRepository repository, AsyncContext context, HttpServletRequest request, HttpServletResponse response, AnyLongObjectId id) throws FileNotFoundException, IOException { this.context = context; this.response = response; this.in = request.getInputStream(); this.inChannel = Channels.newChannel(in); this.out = repository.getOutputStream(id); this.channel = Channels.newChannel(out); response.setContentType(Constants.CONTENT_TYPE_GIT_LFS_JSON); }
ReadableByteChannel getReadChannel(AnyLongObjectId id) throws IOException { return FileChannel.open(getPath(id), StandardOpenOption.READ); }
/** {@inheritDoc} */ @Override public Action getUploadAction(AnyLongObjectId id, long size) { return getAction(id); }
private static char[] toHexCharArray(int b) { final char[] dst = new char[2]; formatHexChar(dst, 0, b); return dst; }
/** {@inheritDoc} */ @Override public long getSize(AnyLongObjectId id) throws IOException { Path p = getPath(id); if (Files.exists(p)) { return Files.size(p); } else { return -1; } }
@Override public Response.Action getDownloadAction(AnyLongObjectId id) { return getAction(id); }
@Override public long getSize(AnyLongObjectId id) throws IOException { Path p = getPath(id); if (Files.exists(p)) { return Files.size(p); } else { return -1; } }
@Override public Action getUploadAction(AnyLongObjectId id, long size) { return getAction(id); }
AtomicObjectOutputStream getOutputStream(AnyLongObjectId id) throws IOException { Path path = getPath(id); Path parent = path.getParent(); if (parent != null) { Files.createDirectories(parent); } return new AtomicObjectOutputStream(path, id); }
AtomicObjectOutputStream getOutputStream(AnyLongObjectId id) throws IOException { Path path = getPath(id); Path parent = path.getParent(); if (parent != null) { Files.createDirectories(parent); } return new AtomicObjectOutputStream(path, id); }