Add experimental post filetype and (byte)size display

gh-actions
neobooru 2023-06-13 23:45:55 +02:00
parent 1376ea8fd5
commit 96466b9199
7 changed files with 111 additions and 22 deletions

View File

@ -28,6 +28,7 @@
"dependencies": { "dependencies": {
"axios": "^1.2.2", "axios": "^1.2.2",
"basscss": "^8.1.0", "basscss": "^8.1.0",
"byte-size": "^8.1.1",
"deepmerge": "^4.3.1", "deepmerge": "^4.3.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"markdown-it": "^13.0.1", "markdown-it": "^13.0.1",
@ -41,6 +42,7 @@
"@fortawesome/fontawesome-svg-core": "^6.2.1", "@fortawesome/fontawesome-svg-core": "^6.2.1",
"@fortawesome/free-solid-svg-icons": "^6.2.1", "@fortawesome/free-solid-svg-icons": "^6.2.1",
"@fortawesome/vue-fontawesome": "^3.0.3", "@fortawesome/vue-fontawesome": "^3.0.3",
"@types/byte-size": "^8.1.0",
"@types/fs-extra": "^9.0.13", "@types/fs-extra": "^9.0.13",
"@types/lodash": "^4.14.194", "@types/lodash": "^4.14.194",
"@types/markdown-it": "^12.2.3", "@types/markdown-it": "^12.2.3",

View File

@ -5,6 +5,7 @@ specifiers:
'@fortawesome/fontawesome-svg-core': ^6.2.1 '@fortawesome/fontawesome-svg-core': ^6.2.1
'@fortawesome/free-solid-svg-icons': ^6.2.1 '@fortawesome/free-solid-svg-icons': ^6.2.1
'@fortawesome/vue-fontawesome': ^3.0.3 '@fortawesome/vue-fontawesome': ^3.0.3
'@types/byte-size': ^8.1.0
'@types/fs-extra': ^9.0.13 '@types/fs-extra': ^9.0.13
'@types/lodash': ^4.14.194 '@types/lodash': ^4.14.194
'@types/markdown-it': ^12.2.3 '@types/markdown-it': ^12.2.3
@ -17,6 +18,7 @@ specifiers:
'@vueuse/core': ^9.6.0 '@vueuse/core': ^9.6.0
axios: ^1.2.2 axios: ^1.2.2
basscss: ^8.1.0 basscss: ^8.1.0
byte-size: ^8.1.1
chokidar: ^3.5.3 chokidar: ^3.5.3
cross-env: ^7.0.3 cross-env: ^7.0.3
crx: ^5.0.1 crx: ^5.0.1
@ -49,6 +51,7 @@ specifiers:
dependencies: dependencies:
axios: 1.4.0 axios: 1.4.0
basscss: 8.1.0 basscss: 8.1.0
byte-size: 8.1.1
deepmerge: 4.3.1 deepmerge: 4.3.1
lodash: 4.17.21 lodash: 4.17.21
markdown-it: 13.0.1 markdown-it: 13.0.1
@ -62,6 +65,7 @@ devDependencies:
'@fortawesome/fontawesome-svg-core': 6.4.0 '@fortawesome/fontawesome-svg-core': 6.4.0
'@fortawesome/free-solid-svg-icons': 6.4.0 '@fortawesome/free-solid-svg-icons': 6.4.0
'@fortawesome/vue-fontawesome': 3.0.3_5vq7vkrflr3ckmegqnktogj5ty '@fortawesome/vue-fontawesome': 3.0.3_5vq7vkrflr3ckmegqnktogj5ty
'@types/byte-size': 8.1.0
'@types/fs-extra': 9.0.13 '@types/fs-extra': 9.0.13
'@types/lodash': 4.14.194 '@types/lodash': 4.14.194
'@types/markdown-it': 12.2.3 '@types/markdown-it': 12.2.3
@ -872,6 +876,10 @@ packages:
engines: {node: '>= 10'} engines: {node: '>= 10'}
dev: true dev: true
/@types/byte-size/8.1.0:
resolution: {integrity: sha512-LCIlZh8vyx+I2fgRycE1D34c33QDppYY6quBYYoaOpQ1nGhJ/avSP2VlrAefVotjJxgSk6WkKo0rTcCJwGG7vA==}
dev: true
/@types/chai-subset/1.3.3: /@types/chai-subset/1.3.3:
resolution: {integrity: sha512-frBecisrNGz+F4T6bcc+NLeolfiojh5FxW2klu669+8BARtyQv2C/GkNW6FUodVe4BroGMP/wER/YDGc7rEllw==} resolution: {integrity: sha512-frBecisrNGz+F4T6bcc+NLeolfiojh5FxW2klu669+8BARtyQv2C/GkNW6FUodVe4BroGMP/wER/YDGc7rEllw==}
dependencies: dependencies:
@ -1687,6 +1695,11 @@ packages:
safe-json-stringify: 1.2.0 safe-json-stringify: 1.2.0
dev: true dev: true
/byte-size/8.1.1:
resolution: {integrity: sha512-tUkzZWK0M/qdoLEqikxBWe4kumyuwjl3HO6zHTr4yEI23EojPtLYXdG1+AQY7MN0cGyNDvEaJ8wiYQm6P2bPxg==}
engines: {node: '>=12.17'}
dev: false
/c12/1.4.1: /c12/1.4.1:
resolution: {integrity: sha512-0x7pWfLZpZsgtyotXtuepJc0rZYE0Aw8PwNAXs0jSG9zq6Sl5xmbWnFqfmRY01ieZLHNbvneSFm9/x88CvzAuw==} resolution: {integrity: sha512-0x7pWfLZpZsgtyotXtuepJc0rZYE0Aw8PwNAXs0jSG9zq6Sl5xmbWnFqfmRY01ieZLHNbvneSFm9/x88CvzAuw==}
dependencies: dependencies:

View File

@ -0,0 +1,29 @@
<script setup lang="ts">
import byteSize from "byte-size";
import { PropType } from "vue";
import { ScrapedPostDetails } from "~/models";
import { resolutionToString } from "~/utils";
defineProps({
modelValue: {
required: true,
type: Object as PropType<ScrapedPostDetails>,
},
});
</script>
<template>
<div class="basic-info-inline">
<span v-if="modelValue.contentSize">{{ byteSize(modelValue.contentSize) }}</span>
<span v-if="modelValue.contentSubType">{{ modelValue.contentSubType }}</span>
<span v-if="modelValue.resolution">({{ resolutionToString(modelValue.resolution) }})</span>
</div>
</template>
<style scoped lang="scss">
.basic-info-inline {
display: flex;
gap: 0 1ch;
flex-wrap: wrap;
}
</style>

View File

@ -47,8 +47,10 @@ export class ScrapedPostDetails {
tags: TagDetails[] = []; tags: TagDetails[] = [];
notes: ScrapedNote[]; notes: ScrapedNote[];
contentUrl: string; contentUrl: string;
contentSize: number | undefined;
pageUrl: string; pageUrl: string;
contentType: ContentType; contentType: ContentType;
contentSubType: string | undefined;
rating: BooruTypes.SafetyRating; rating: BooruTypes.SafetyRating;
source = ""; source = "";
referrer?: string; referrer?: string;
@ -57,6 +59,7 @@ export class ScrapedPostDetails {
constructor(post: ScrapedPost) { constructor(post: ScrapedPost) {
this.contentUrl = post.contentUrl; this.contentUrl = post.contentUrl;
// this.contentSize = post.contentSize;
this.pageUrl = post.pageUrl; this.pageUrl = post.pageUrl;
this.contentType = post.contentType; this.contentType = post.contentType;
this.rating = post.rating; this.rating = post.rating;
@ -118,11 +121,7 @@ export class PostUploadCommandData {
} }
export class SetPostUploadInfoData { export class SetPostUploadInfoData {
constructor( constructor(public instanceId: string, public postId: string, public info: PostUploadInfo) {}
public instanceId: string,
public postId: string,
public info: PostUploadInfo
) {}
} }
export class SetExactPostId { export class SetExactPostId {
@ -134,5 +133,9 @@ export class SetExactPostId {
} }
export class PostUpdateCommandData { export class PostUpdateCommandData {
constructor(public readonly postId: number, public readonly updateRequest: UpdatePostRequest, public readonly selectedSite: SzuruSiteConfig) {} constructor(
public readonly postId: number,
public readonly updateRequest: UpdatePostRequest,
public readonly selectedSite: SzuruSiteConfig
) {}
} }

View File

@ -2,7 +2,7 @@
import { useDark } from "@vueuse/core"; import { useDark } from "@vueuse/core";
import { cloneDeep } from "lodash"; import { cloneDeep } from "lodash";
import { ScrapeResults } from "neo-scraper"; import { ScrapeResults } from "neo-scraper";
import { getUrl, encodeTagName, getErrorMessage } from "~/utils"; import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary } from "~/utils";
import { import {
BrowserCommand, BrowserCommand,
ScrapedPostDetails, ScrapedPostDetails,
@ -124,8 +124,10 @@ async function grabPost() {
if (cfg.value.loadTagCounts) { if (cfg.value.loadTagCounts) {
loadTagCounts(); loadTagCounts();
} }
} else {
// pushInfo("No posts found."); if (cfg.value.fetchPostInfo) {
fetchPostInfo();
}
} }
} }
@ -154,13 +156,6 @@ async function upload() {
} }
} }
function resolutionToString(resolution: [number, number]) {
if (resolution && resolution.length == 2) {
return resolution[0] + "x" + resolution[1];
}
return "";
}
function removeTag(tag: TagDetails) { function removeTag(tag: TagDetails) {
if (pop.selectedPost) { if (pop.selectedPost) {
const idx = pop.selectedPost.tags.indexOf(tag); const idx = pop.selectedPost.tags.indexOf(tag);
@ -275,6 +270,29 @@ async function loadTagCounts() {
} }
} }
async function fetchPostInfo() {
for (const post of pop.posts) {
if (!post.contentSize) {
// We are missing cookies/etc which means that the request might fail.
// If you want access to the cookies/session/etc then you need to execute this code inside the content script.
try {
const res = await fetch(post.contentUrl, { method: "HEAD" });
const size = res.headers.get("Content-Length");
const type = res.headers.get("Content-Type");
if (size) post.contentSize = parseInt(size);
if (type) {
const [_main, sub] = type.split("/");
if (sub) post.contentSubType = sub.toUpperCase();
}
} catch {
// TODO: Log errors.
}
}
}
}
// Cool people would use https://kazupon.github.io/vue-i18n/guide/pluralization.html // Cool people would use https://kazupon.github.io/vue-i18n/guide/pluralization.html
function getUpdatedTagsText(count: number) { function getUpdatedTagsText(count: number) {
return `Updated ${count} tag${count > 1 ? "s" : ""}`; return `Updated ${count} tag${count > 1 ? "s" : ""}`;
@ -438,10 +456,8 @@ useDark();
<font-awesome-icon icon="fa-solid fa-cog" class="cursor-pointer" @click="openOptionsPage" /> <font-awesome-icon icon="fa-solid fa-cog" class="cursor-pointer" @click="openOptionsPage" />
</template> </template>
<div class="section-row" v-if="pop.selectedPost?.resolution"> <div class="section-row" v-if="pop.selectedPost?.resolution || pop.selectedPost?.contentSize">
<ul class="compact"> <PostInfoSummary v-model="pop.selectedPost" />
<li>Resolution: {{ resolutionToString(pop.selectedPost.resolution) }}</li>
</ul>
</div> </div>
<div class="section-row"> <div class="section-row">
@ -500,7 +516,10 @@ useDark();
<div class="popup-right"> <div class="popup-right">
<div class="popup-section"> <div class="popup-section">
<select v-model="pop.selectedPostId"> <select v-model="pop.selectedPostId">
<option v-for="post in pop.posts" :key="post.id" :value="post.id">{{ post.name }}</option> <!-- &#160;&#160; is used because duplicate space characters are usually removed by the browser. -->
<option v-for="post in pop.posts" :key="post.id" :value="post.id">
{{ post.name }} &#160;-&#160; {{ getPostInfoSummary(post) }}
</option>
</select> </select>
</div> </div>

View File

@ -10,6 +10,7 @@ export const cfg = useStorageLocal(
addPageUrlToSource: true, addPageUrlToSource: true,
autoSearchSimilar: false, autoSearchSimilar: false,
loadTagCounts: true, loadTagCounts: true,
fetchPostInfo: true,
useContentTokens: true, useContentTokens: true,
sites: [] as Array<SzuruSiteConfig>, sites: [] as Array<SzuruSiteConfig>,
selectedSiteId: undefined as string | undefined, selectedSiteId: undefined as string | undefined,

View File

@ -1,5 +1,6 @@
import byteSize from "byte-size";
import { MicroUser, Post } from "./api/models"; import { MicroUser, Post } from "./api/models";
import { TagDetails } from "./models"; import { ScrapedPostDetails, TagDetails } from "./models";
export function getUrl(root: string, ...parts: string[]): string { export function getUrl(root: string, ...parts: string[]): string {
let url = root.replace(/\/+$/, ""); let url = root.replace(/\/+$/, "");
@ -80,3 +81,24 @@ export function breakTagName(tagName: string) {
// Based on https://stackoverflow.com/a/6316913 // Based on https://stackoverflow.com/a/6316913
return tagName.replace(/_/g, "_<wbr>"); return tagName.replace(/_/g, "_<wbr>");
} }
export function resolutionToString(resolution: [number, number]) {
if (resolution && resolution.length == 2) {
return resolution[0] + "x" + resolution[1];
}
return "";
}
export function getPostInfoSummary(post: ScrapedPostDetails) {
const parts = [];
if (post.contentSize) {
parts.push(byteSize(post.contentSize));
}
if (post.contentSubType) {
parts.push(post.contentSubType);
}
if (post.resolution) {
parts.push(resolutionToString(post.resolution));
}
return parts.join(" / ");
}