Search in sources :

Example 1 with MimeType

use of eu.medsea.mimeutil.MimeType in project gerrit by GerritCodeReview.

the class CommentContextLoader method getContentType.

private String getContentType(TreeWalk tw, String filePath, Text src) {
    PatchScript.FileMode fileMode = PatchScript.FileMode.fromJgitFileMode(tw.getFileMode(0));
    String mimeType = MimeUtil2.UNKNOWN_MIME_TYPE.toString();
    if (src.size() > 0 && PatchScript.FileMode.SYMLINK != fileMode) {
        MimeType registryMimeType = registry.getMimeType(filePath, src.getContent());
        mimeType = registryMimeType.toString();
    }
    return FileContentUtil.resolveContentType(projectState, filePath, fileMode, mimeType);
}
Also used : PatchScript(com.google.gerrit.common.data.PatchScript) MimeType(eu.medsea.mimeutil.MimeType)

Example 2 with MimeType

use of eu.medsea.mimeutil.MimeType in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetServiceImpl method getMimeType.

protected void getMimeType(InputStream inputStream, String fileName, StaticAsset newAsset) {
    Collection mimeTypes = MimeUtil.getMimeTypes(fileName);
    if (!mimeTypes.isEmpty()) {
        MimeType mimeType = (MimeType) mimeTypes.iterator().next();
        newAsset.setMimeType(mimeType.toString());
    } else {
        mimeTypes = MimeUtil.getMimeTypes(inputStream);
        if (!mimeTypes.isEmpty()) {
            MimeType mimeType = (MimeType) mimeTypes.iterator().next();
            newAsset.setMimeType(mimeType.toString());
        }
    }
}
Also used : Collection(java.util.Collection) MimeType(eu.medsea.mimeutil.MimeType)

Example 3 with MimeType

use of eu.medsea.mimeutil.MimeType in project gerrit by GerritCodeReview.

the class FileContentUtil method downloadContent.

public BinaryResult downloadContent(ProjectState project, ObjectId revstr, String path, @Nullable Integer parent) throws ResourceNotFoundException, IOException {
    try (Repository repo = openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        String suffix = "new";
        RevCommit commit = rw.parseCommit(revstr);
        if (parent != null && parent > 0) {
            if (commit.getParentCount() == 1) {
                suffix = "old";
            } else {
                suffix = "old" + parent;
            }
            commit = rw.parseCommit(commit.getParent(parent - 1));
        }
        try (TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), path, commit.getTree())) {
            if (tw == null) {
                throw new ResourceNotFoundException();
            }
            int mode = tw.getFileMode(0).getObjectType();
            if (mode != Constants.OBJ_BLOB) {
                throw new ResourceNotFoundException();
            }
            ObjectId id = tw.getObjectId(0);
            ObjectLoader obj = repo.open(id, OBJ_BLOB);
            byte[] raw;
            try {
                raw = obj.getCachedBytes(MAX_SIZE);
            } catch (LargeObjectException e) {
                raw = null;
            }
            MimeType contentType = registry.getMimeType(path, raw);
            return registry.isSafeInline(contentType) ? wrapBlob(path, obj, raw, contentType, suffix) : zipBlob(path, obj, commit, suffix);
        }
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) MimeType(eu.medsea.mimeutil.MimeType) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with MimeType

use of eu.medsea.mimeutil.MimeType in project gerrit by GerritCodeReview.

the class DefaultFileExtensionRegistry method getMimeTypesFileName.

@Override
protected Collection<MimeType> getMimeTypesFileName(String name) {
    int s = name.lastIndexOf('/');
    if (s >= 0) {
        name = name.substring(s + 1);
    }
    MimeType type = TYPES.get(name);
    if (type != null) {
        return Collections.singletonList(type);
    }
    int d = name.lastIndexOf('.');
    if (0 < d) {
        type = TYPES.get(name.substring(d + 1));
        if (type != null) {
            return Collections.singletonList(type);
        }
    }
    return Collections.emptyList();
}
Also used : MimeType(eu.medsea.mimeutil.MimeType)

Example 5 with MimeType

use of eu.medsea.mimeutil.MimeType in project lobcder by skoulouzis.

the class ContentTypeUtils method buildContentTypeText.

private static String buildContentTypeText(Collection mimeTypes) {
    StringBuilder sb = null;
    for (Object o : mimeTypes) {
        MimeType mt = (MimeType) o;
        if (sb == null) {
            sb = new StringBuilder();
        } else {
            sb.append(",");
        }
        sb.append(mt.toString());
    }
    if (sb == null) {
        return "";
    } else {
        return sb.toString();
    }
}
Also used : MimeType(eu.medsea.mimeutil.MimeType)

Aggregations

MimeType (eu.medsea.mimeutil.MimeType)5 PatchScript (com.google.gerrit.common.data.PatchScript)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 Collection (java.util.Collection)1 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1