/** * @param db the repository * @param in an {@link InputStream} providing the original content * @param out the {@link OutputStream} into which the content of the pointer file should be * written. That's the content which will be added to the git repository * @throws IOException when the creation of the temporary file fails or when no {@link * OutputStream} for this file can be created */ public CleanFilter(Repository db, InputStream in, OutputStream out) throws IOException { super(in, out); lfsUtil = new Lfs(db.getDirectory().toPath().resolve("lfs")); // $NON-NLS-1$ Files.createDirectories(lfsUtil.getLfsTmpDir()); tmpFile = lfsUtil.createTmpFile(); tmpOut = Files.newOutputStream(tmpFile, StandardOpenOption.CREATE); this.dOut = new DigestOutputStream(tmpOut, Constants.newMessageDigest()); }
public int run() throws IOException { try { int b = in.read(); if (b != -1) { dOut.write(b); size++; return 1; } else { dOut.close(); tmpOut.close(); LongObjectId loid = LongObjectId.fromRaw(dOut.getMessageDigest().digest()); Path mediaFile = lfsUtil.getMediaFile(loid); if (Files.isRegularFile(mediaFile)) { long fsSize = Files.size(mediaFile); if (fsSize != size) { throw new CorruptMediaFile(mediaFile, size, fsSize); } } else { FileUtils.mkdirs(mediaFile.getParent().toFile(), true); FileUtils.rename(tmpFile.toFile(), mediaFile.toFile()); } LfsPointer lfsPointer = new LfsPointer(loid, size); lfsPointer.encode(out); out.close(); return -1; } } catch (IOException e) { out.close(); dOut.close(); tmpOut.close(); throw e; } }