Skip to content

Commit

Permalink
added function mimetypes/probe-content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Mar 31, 2024
1 parent 160b872 commit 38d79be
Show file tree
Hide file tree
Showing 6 changed files with 1,697 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ All notable changes to this project will be documented in this file.
threads
- support for tracing exceptions in `trace/trace-var`
- math function `rand-bigint`

- function `mimetypes/probe-content-type`


## [1.12.7] - 2024-03-26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.github.jlangch.venice.impl.docgen.cheatsheet.modules.ModuleKiraSection;
import com.github.jlangch.venice.impl.docgen.cheatsheet.modules.ModuleMatrixSection;
import com.github.jlangch.venice.impl.docgen.cheatsheet.modules.ModuleMavenSection;
import com.github.jlangch.venice.impl.docgen.cheatsheet.modules.ModuleMimetypesSection;
import com.github.jlangch.venice.impl.docgen.cheatsheet.modules.ModuleParsifalSection;
import com.github.jlangch.venice.impl.docgen.cheatsheet.modules.ModuleQrRefSection;
import com.github.jlangch.venice.impl.docgen.cheatsheet.modules.ModuleRingSection;
Expand Down Expand Up @@ -136,7 +137,7 @@ public DocGenerator(final boolean runExamples) {
"fonts", "qrref", "jsonl", "timing",
"zipvault", "gradlew", "matrix", "ascii-table",
"docker", "cargo", "cargo-arangodb", "cargo-qdrant",
"installer", "tomcat", "ring"));
"installer", "tomcat", "ring", "mimetypes"));

final IVeniceInterpreter venice = new VeniceInterpreter(new AcceptAllInterceptor());

Expand Down Expand Up @@ -343,33 +344,39 @@ private List<DocSection> getTOC() {
final DocSection extmod = new DocSection("Modules", "modules");
extmod.addSection(new DocSection("Kira\u00A0Templates", "modules.kira"));
extmod.addSection(new DocSection("Parsifal", "modules.parsifal"));
extmod.addSection(new DocSection("Grep", "modules.grep"));
extmod.addSection(new DocSection("Configuration", "modules.config"));
extmod.addSection(new DocSection("Component", "modules.component"));
extmod.addSection(new DocSection("ZipVault", "modules.zipvault"));
extmod.addSection(new DocSection("XML", "modules.xml"));
extmod.addSection(new DocSection("Grep", "modules.grep"));
extmod.addSection(new DocSection("Fonts", "modules.fonts"));
extmod.addSection(new DocSection("Cryptography", "modules.cryptography"));
extmod.addSection(new DocSection("AsciiTable", "modules.asciitable"));
extmod.addSection(new DocSection("Hexdump", "modules.hexdump"));
extmod.addSection(new DocSection("Matrix", "modules.matrix"));
extmod.addSection(new DocSection("Java", "modules.java"));
extmod.addSection(new DocSection("Hexdump", "modules.hexdump"));
extmod.addSection(new DocSection("Shell", "modules.shell"));
extmod.addSection(new DocSection("Geo IP", "modules.geoip"));
extmod.addSection(new DocSection("Mimetypes", "modules.mimetypes"));
extmod.addSection(new DocSection("Ansi", "modules.ansi"));
extmod.addSection(new DocSection("Gradle\u00A0Wrapper", "modules.gradlew"));
extmod.addSection(new DocSection("Gradle", "modules.gradle"));
extmod.addSection(new DocSection("Maven", "modules.maven"));
extmod.addSection(new DocSection("Test", "modules.test"));
extmod.addSection(new DocSection("Tracing", "modules.tracing"));
extmod.addSection(new DocSection("Benchmark", "modules.benchmark"));
extmod.addSection(new DocSection("Timing", "modules.timing"));
extmod.addSection(new DocSection("App", "modules.app"));
extmod.addSection(new DocSection("QR\u00A0Ref", "modules.qrref"));
extmod.addSection(new DocSection("Installer", "modules.installer"));
extmod.addSection(new DocSection("Semver", "modules.semver"));
content.add(extmod);

final DocSection build = new DocSection("Build\u00A0Tools", "build");
build.addSection(new DocSection("Gradle\u00A0Wrapper", "modules.gradlew"));
build.addSection(new DocSection("Gradle", "modules.gradle"));
build.addSection(new DocSection("Maven", "modules.maven"));
build.addSection(new DocSection("Installer", "modules.installer"));
content.add(build);

final DocSection debug = new DocSection("Test\u00A0&\u00A0Debug", "test");
debug.addSection(new DocSection("Test", "modules.test"));
debug.addSection(new DocSection("Tracing", "modules.tracing"));
debug.addSection(new DocSection("Timing", "modules.timing"));
debug.addSection(new DocSection("Benchmark", "modules.benchmark"));
content.add(debug);

final DocSection web = new DocSection("Web", "web");
web.addSection(new DocSection("Http\u00A0Client", "modules.http-client"));
web.addSection(new DocSection("Http\u00A0Client\u00A0Legacy", "modules.http-client-legacy"));
Expand Down Expand Up @@ -483,6 +490,7 @@ private List<DocSection> getModulesLeftSections() {
new ModuleRingSection(diBuilder).section(),
new ModuleTracingSection(diBuilder).section(),
new ModuleShellSection(diBuilder).section(),
new ModuleMimetypesSection(diBuilder).section(),
new ModuleAnsiSection(diBuilder).section(),
new ModuleInstallerSection(diBuilder).section());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* __ __ _
* \ \ / /__ _ __ (_) ___ ___
* \ \/ / _ \ '_ \| |/ __/ _ \
* \ / __/ | | | | (_| __/
* \/ \___|_| |_|_|\___\___|
*
*
* Copyright 2017-2024 Venice
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jlangch.venice.impl.docgen.cheatsheet.modules;

import com.github.jlangch.venice.impl.docgen.cheatsheet.DocItemBuilder;
import com.github.jlangch.venice.impl.docgen.cheatsheet.DocSection;
import com.github.jlangch.venice.impl.docgen.cheatsheet.ISectionBuilder;


public class ModuleMimetypesSection implements ISectionBuilder {

public ModuleMimetypesSection(final DocItemBuilder diBuilder) {
this.diBuilder = diBuilder;
}

@Override
public DocSection section() {
final DocSection section = new DocSection(
"Mimetypes",
"modules.mimetypes");

final DocSection all = new DocSection("(load-module :mimetypes)", id());
section.addSection(all);

final DocSection hexdump = new DocSection("Mimetypes", id());
all.addSection(hexdump);
hexdump.addItem(diBuilder.getDocItem("mimetypes/probe-content-type", true));

return section;
}

private String id() {
return diBuilder.id();
}

private final DocItemBuilder diBuilder;
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static boolean isValidModule(final VncKeyword module) {
"maven",
"mercator",
"mimetypes",
"multipart",
"parsifal",
"qrbill",
"qrref",
Expand Down
42 changes: 42 additions & 0 deletions src/main/resources/com/github/jlangch/venice/mimetypes.venice
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,45 @@
:image-svg+xml "image/svg+xml"
:email "message/rfc822"
})


(defonce ^:private mime-types-resource "com/github/jlangch/venice/mimetypes/mime.types")

(defn- load-mime-types-resources []
(as-> (io/load-classpath-resource mime-types-resource) v
(bytebuf-to-string v :UTF-8)
(str/split-lines v)
(filter #(not (str/starts-with? % "#")) v)
(map #(str/split % "(\t| )+") v)
(map #(cartesian-product [(first %)] (rest %)) v)
(mapcat #(if (sequential? %) % [%]) v)
(map #(vector (second %) (keyword (first %))) v)
(into {} v)))

(defonce ^:private load-content-types
(delay (load-mime-types-resources)))


(defn
^{ :arglists '("(probe-content-type f)")
:doc """
Probes the content type of a file.

The function uses built-in "mime.types" data file to lookup the
file's mimetype based on the file extension.

f must be a string or a :java.io.File.

Returns `nil` if a mapping is not defined.
"""
:examples '(
"""
(do
(load-module :mimetypes)
(mimetypes/probe-content-type "foo.png"))
""") }

probe-content-type [f]

(assert (or (string? f) (io/file? f)))
(get (force load-content-types) (io/file-ext f)))
Loading

0 comments on commit 38d79be

Please sign in to comment.