Add .odt files support

This commit is contained in:
Nikita Ivanov 2022-06-17 21:06:31 +05:00
parent fa00d9ff6f
commit ff9518de9d
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
5 changed files with 42 additions and 11 deletions

15
prev/odt.sh Normal file
View File

@ -0,0 +1,15 @@
# libreoffice
office() {
# File produced by libreoffice
jpg="$(printf '%s\n' "$f" | sed 's|^.*/||; s|\..*$||')"
libreoffice \
--headless \
--convert-to jpg "$f" \
--outdir "$cache_d" >/dev/null &&
mv "$cache_d/$jpg.jpg" "$cache_f"
}
convert_and_show_image office

View File

@ -19,6 +19,7 @@ Preview b_previews[] = {
PR("md", NULL, NULL, mdcat),
PR("torrent", NULL, NULL, torrent),
PR("odt", NULL, NULL, odt),
PR(NULL, "text", NULL, bat),
PR(NULL, "text", NULL, highlight),

View File

@ -152,14 +152,20 @@ static void md5_string(char *buf, size_t len, char *s)
}
}
static int get_cache_file(char *buf, size_t len, char *file)
static int get_cache_file(char *dir, size_t dir_len, char *filename,
size_t filename_len, char *file)
{
ERRCHK_RET_OK(get_cache_dir(buf, len, "ctpv/"));
ERRCHK_RET_OK(create_dir(buf, len));
ERRCHK_RET_OK(get_cache_dir(dir, dir_len, "ctpv/"));
ERRCHK_RET_OK(create_dir(dir, dir_len));
char name[64];
md5_string(name, LEN(name) - 1, file);
strncat(buf, name, len - 1);
size_t dir_str_len = strlen(dir);
memcpy(filename, dir, filename_len);
md5_string(filename + dir_str_len, filename_len - dir_str_len - 1, file);
/* Remove dash at the end */
dir[dir_str_len-1] = '\0';
return OK;
}
@ -195,15 +201,23 @@ static int preview(int argc, char *argv[])
const char *mimetype;
ERRCHK_RET(!(mimetype = get_mimetype(f)));
char cache_file[FILENAME_MAX];
ERRCHK_RET_OK(get_cache_file(cache_file, LEN(cache_file), f));
char cache_dir[FILENAME_MAX], cache_file[FILENAME_MAX];
ERRCHK_RET_OK(get_cache_file(cache_dir, LEN(cache_file), cache_file,
LEN(cache_file), f));
int cache_valid;
ERRCHK_RET_OK(check_cache(&cache_valid, f, cache_file));
PreviewArgs args = {
.f = f, .w = w, .h = h, .x = x, .y = y, .id = id,
.cache_file = cache_file, .cache_valid = cache_valid,
.f = f,
.w = w,
.h = h,
.x = x,
.y = y,
.id = id,
.cache_dir = cache_dir,
.cache_file = cache_file,
.cache_valid = cache_valid,
};
return preview_run(get_ext(f), mimetype, &args);

View File

@ -166,6 +166,7 @@ int preview_run(const char *ext, const char *mimetype, PreviewArgs *pa)
SET_PENV("y", pa->y);
SET_PENV("id", pa->id);
SET_PENV("cache_f", pa->cache_file);
SET_PENV("cache_d", pa->cache_dir);
{
char *s = pa->cache_valid ? "1" : "";

View File

@ -17,7 +17,7 @@ VECTOR_GEN_HEADER(Preview, Preview)
typedef struct {
char *f, *w, *h, *x, *y, *id;
char *cache_file;
char *cache_file, *cache_dir;
int cache_valid;
} PreviewArgs;