parent
d1d8771348
commit
8f0fa5428c
@ -0,0 +1,24 @@
|
|||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra
|
||||||
|
|
||||||
|
all: test
|
||||||
|
|
||||||
|
test: testGuess
|
||||||
|
./testGuess
|
||||||
|
|
||||||
|
testGuess: build/mimetype.o build/testGuess.o
|
||||||
|
$(CC) -o $@ $^
|
||||||
|
|
||||||
|
build/mimetype.o: src/mimetype.c src/mimetype.h | build
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
build/testGuess.o: test/testGuess.c src/mimetype.h | build
|
||||||
|
$(CC) $(CFLAGS) -Isrc/ -c $< -o $@
|
||||||
|
|
||||||
|
build:
|
||||||
|
mkdir build
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf testGuess build
|
||||||
|
|
||||||
|
.PHONY: all test clean
|
@ -0,0 +1,5 @@
|
|||||||
|
[package]
|
||||||
|
name = "guess-mime-type"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Guess mime type from a filename"
|
||||||
|
license = "MIT"
|
@ -0,0 +1,41 @@
|
|||||||
|
#include "mimetype.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
const char *guessMimeType(const char *filename) {
|
||||||
|
const char *dot = strrchr(filename, '.');
|
||||||
|
if (!dot || dot == filename) {
|
||||||
|
return "application/octet-stream";
|
||||||
|
}
|
||||||
|
const char *extension = dot + 1;
|
||||||
|
if (!extension) {
|
||||||
|
return "text/plain";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp("avif", extension) == 0) {
|
||||||
|
return "image/avif";
|
||||||
|
} else if (strcmp("css", extension) == 0) {
|
||||||
|
return "text/css";
|
||||||
|
} else if (strcmp("csv", extension) == 0) {
|
||||||
|
return "text/csv";
|
||||||
|
} else if (strcmp("gif", extension) == 0) {
|
||||||
|
return "image/gif";
|
||||||
|
} else if (strcmp("html", extension) == 0) {
|
||||||
|
return "text/html";
|
||||||
|
} else if (strcmp("jpg", extension) == 0 || strcmp("jpeg", extension) == 0) {
|
||||||
|
return "image/jpeg";
|
||||||
|
} else if (strcmp("js", extension) == 0) {
|
||||||
|
return "application/javascript";
|
||||||
|
} else if (strcmp("json", extension) == 0) {
|
||||||
|
return "application/json";
|
||||||
|
} else if (strcmp("pdf", extension) == 0) {
|
||||||
|
return "application/pdf";
|
||||||
|
} else if (strcmp("png", extension) == 0) {
|
||||||
|
return "image/png";
|
||||||
|
} else if (strcmp("svg", extension) == 0) {
|
||||||
|
return "image/svg+xml";
|
||||||
|
} else if (strcmp("webp", extension) == 0) {
|
||||||
|
return "image/webp";
|
||||||
|
}
|
||||||
|
return "text/plain";
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef GUESS_MIME_TYPE_H
|
||||||
|
#define GUESS_MIME_TYPE_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guess the mime type of a file from its extension.
|
||||||
|
*
|
||||||
|
* @param filename The file name
|
||||||
|
* @return The mime type deduced from common web extensions
|
||||||
|
*/
|
||||||
|
const char *guessMimeType(const char *filename);
|
||||||
|
|
||||||
|
#endif // GUESS_MIME_TYPE_H
|
@ -0,0 +1,14 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "mimetype.h"
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
assert(strcmp(guessMimeType("/tmp/file"), "application/octet-stream") == 0);
|
||||||
|
assert(strcmp(guessMimeType("www/car.avif"), "image/avif") == 0);
|
||||||
|
assert(strcmp(guessMimeType("css.html"), "text/html") == 0);
|
||||||
|
assert(strcmp(guessMimeType("html.css.js"), "application/javascript") == 0);
|
||||||
|
printf("Success!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in new issue