123 lines
2.7 KiB
C
123 lines
2.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <magic.h>
|
|
|
|
#include "backend.h"
|
|
#include "defines.h"
|
|
#include "config.h"
|
|
|
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
|
static FILE *ueberzug = NULL;
|
|
#endif
|
|
extern unsigned int terminal_height;
|
|
extern unsigned int terminal_width;
|
|
char previewd;
|
|
magic_t cookie_type;
|
|
magic_t cookie_description;
|
|
|
|
char* text(file *f);
|
|
void images_print(char *file_name);
|
|
void images_clear();
|
|
char* generic(file *f);
|
|
|
|
char* get_mimetype(file *f){
|
|
char *mime = (char*)magic_file(cookie_type, f->file_name);
|
|
|
|
return mime;
|
|
}
|
|
char* preview_file(file *f){
|
|
|
|
char *file_buffer;
|
|
|
|
char *mime = get_mimetype(f);
|
|
|
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
|
images_clear();
|
|
#endif
|
|
|
|
if (mime != NULL) {
|
|
if (strstr(mime, "text")) {
|
|
file_buffer = text(f);
|
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
|
} else if (strstr(mime, "image")) {
|
|
file_buffer = generic(f);
|
|
images_print(f->file_name);
|
|
previewd = 1;
|
|
#endif
|
|
} else {
|
|
file_buffer = generic(f);
|
|
}
|
|
} else {
|
|
file_buffer = generic(f);
|
|
}
|
|
|
|
/*free(mime); seems like magic_file handles the free too*/
|
|
return file_buffer;
|
|
|
|
}
|
|
char* text(file *f){
|
|
|
|
unsigned long size = (terminal_width/2) * terminal_height;
|
|
if (size > f->file_size) {
|
|
size = f->file_size;
|
|
}
|
|
char *file_buffer = malloc(size + 1);
|
|
FILE *fp = fopen(f->file_name, "r");
|
|
if (fread(file_buffer, size, 1, fp) != 0) {
|
|
fclose(fp);
|
|
file_buffer[size] = '\0';
|
|
return file_buffer;
|
|
} else {
|
|
fclose(fp);
|
|
return "failed reading file";
|
|
}
|
|
}
|
|
char* generic(file *f){
|
|
char *mime = (char*)magic_file(cookie_description, f->file_name);
|
|
char *buffer = malloc(strlen(mime)+1);
|
|
memcpy(buffer, mime, strlen(mime)+1);
|
|
return buffer;
|
|
|
|
}
|
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
|
void images_clear(){
|
|
if (previewd == 1) {
|
|
fprintf(ueberzug, "{\"action\": \"remove\", \
|
|
\"identifier\": \"preview\"}\n");
|
|
fflush(ueberzug);
|
|
previewd = 0;
|
|
}
|
|
}
|
|
void images_print(char *file_name){
|
|
char *path=getcwd(NULL, 0);
|
|
|
|
fprintf(ueberzug, "{\"action\":\"add\", \
|
|
\"identifier\":\"preview\", \
|
|
\"max_height\":%d, \
|
|
\"max_width\":%d, \
|
|
\"y\":0, \
|
|
\"x\":%d, \
|
|
\"path\":\"%s/%s\"}\n", terminal_height, terminal_width/2, terminal_width/2, path, file_name);
|
|
fflush(ueberzug);
|
|
free(path);
|
|
}
|
|
void ueberzug_init(){
|
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW == 2
|
|
ueberzug = popen("ueberzug layer -s ", "w");
|
|
#elif SETTINGS_UEBERZUG_IMAGE_PREVIEW == 1
|
|
ueberzug = popen("ueberzug layer -s --no-cache ", "w");
|
|
#endif
|
|
}
|
|
void file_preview_init(){
|
|
cookie_type = magic_open(MAGIC_MIME_TYPE);
|
|
cookie_description = magic_open(MAGIC_NONE);
|
|
magic_load(cookie_type, NULL);
|
|
magic_load(cookie_description, NULL);
|
|
}
|
|
void ueberzug_close(){
|
|
images_clear();
|
|
pclose(ueberzug);
|
|
}
|
|
#endif
|