Skip to content

Commit

Permalink
fix: check server capabilities before calling them
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Jan 27, 2022
1 parent fcc50a1 commit c0cec92
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 17 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ plugins {
}

configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
outputToConsole.set(true)
disabledRules.set(setOf("no-wildcard-imports"))
}

Expand Down
1 change: 1 addition & 0 deletions src/jelu-ui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ store.dispatch('getUser')
console.log("entrypoint " + store.state.entryPoint)
await router.push({ path: store.state.entryPoint })
console.log("ok nav")
store.dispatch('getServerSettings')
// } catch(e) {
// console.log("error nav")
// console.log(e)
Expand Down
16 changes: 15 additions & 1 deletion src/jelu-ui/src/components/AddBook.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script setup lang="ts">
import { computed, reactive, Ref, ref } from "vue";
import { useStore } from 'vuex'
import { UserBook } from "../model/Book";
import dataService from "../services/DataService";
import { StringUtils } from "../utils/StringUtils";
import { useProgrammatic } from "@oruga-ui/oruga-next";
import { key } from '../store'
import { Author } from "../model/Author";
import { Tag } from "../model/Tag";
import AutoImportFormModalVue from "./AutoImportFormModal.vue";
Expand All @@ -12,6 +14,7 @@ import { ObjectUtils } from "../utils/ObjectUtils";
import IsbnVerify from '@saekitominaga/isbn-verify';
import Swal from 'sweetalert2';
const store = useStore(key)
const { oruga } = useProgrammatic();
const datepicker = ref(null);
Expand Down Expand Up @@ -396,6 +399,16 @@ async function checkIsbnExists(isbn10: string, isbn13: string) {
}
return null
}
let autoImportPopupContent = computed(() => {
if (store.state.serverSettings.metadataFetchEnabled) {
return "Try to auto fill some fields from the web, given a isbn or a title"
}
else {
return "Auto import requires Calibre to be installed"
}
})
</script>

<template>
Expand All @@ -408,11 +421,12 @@ async function checkIsbnExists(isbn10: string, isbn13: string) {
</div>
<div class="column is-one-fifth">
<o-tooltip
label="Try to auto fill some fields from the web, given a isbn or a title"
:label="autoImportPopupContent"
multiline
>
<button
class="button is-success is-light"
:disabled="!store.state.serverSettings.metadataFetchEnabled"
@click="toggleModal"
>
Auto fill
Expand Down
4 changes: 4 additions & 0 deletions src/jelu-ui/src/model/ServerSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ServerSettings {
metadataFetchEnabled: boolean,
metadataFetchCalibreEnabled: boolean,
}
19 changes: 19 additions & 0 deletions src/jelu-ui/src/services/DataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Tag } from "../model/Tag";
import { Metadata } from "../model/Metadata";
import { Page } from "../model/Page";
import { Quote } from "../model/Quote";
import { ServerSettings } from "../model/ServerSettings";

class DataService {

Expand All @@ -33,6 +34,8 @@ class DataService {

private API_READING_EVENTS = '/reading-events';

private API_SERVER_SETTINGS = '/server-settings';

private MODE: string;

private BASE_URL: string;
Expand Down Expand Up @@ -595,6 +598,22 @@ class DataService {
}
}

serverSettings = async () => {
try {
const response = await this.apiClient.get<ServerSettings>(`${this.API_SERVER_SETTINGS}`);
console.log("called server settings")
console.log(response)
return response.data;
}
catch (error) {
if (axios.isAxiosError(error) && error.response) {
console.log("error axios " + error.response.status + " " + error.response.data.error)
}
console.log("error server settings " + (error as AxiosError).code)
throw new Error("error server settings " + error)
}
}

}


Expand Down
35 changes: 29 additions & 6 deletions src/jelu-ui/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { UserAuthentication, User } from './model/User'
import dataService from './services/DataService'
import router from './router'
import { InjectionKey } from 'vue'
import { ServerSettings } from './model/ServerSettings'

export interface State {
count: number,
isLogged: boolean,
isInitialSetup : boolean,
user : User | null,
entryPoint: string
entryPoint: string,
serverSettings: ServerSettings
}

export const key: InjectionKey<Store<State>> = Symbol()
Expand All @@ -22,7 +24,11 @@ const store = createStore<State>({
isLogged: false,
isInitialSetup : false,
user: null,
entryPoint: '/'
entryPoint: '/',
serverSettings: {
metadataFetchEnabled: false,
metadataFetchCalibreEnabled: false
} as ServerSettings
}
},
mutations: {
Expand All @@ -41,6 +47,9 @@ const store = createStore<State>({
entryPoint(state, entryPoint: string) {
state.entryPoint = entryPoint
},
serverSettings(state, serverSettings: ServerSettings) {
state.serverSettings = serverSettings
},
},
actions: {
async setupStatus({commit, state}) {
Expand All @@ -59,13 +68,14 @@ const store = createStore<State>({
throw error
}
},
async authenticate({commit, state}, payload) {
async authenticate({dispatch, commit, state}, payload) {
try {
const user: User = await dataService.authenticateUser(payload.user, payload.password)
console.log('store authenticate')
console.log(user)
commit('login', true)
commit('user', user)
dispatch('getServerSettings')
await router.push({path: state.entryPoint})
} catch (error) {
commit('login', false)
Expand All @@ -83,15 +93,28 @@ const store = createStore<State>({
commit('login', false)
commit('user', null)
router.push({name: 'login'})
}
},
async getServerSettings({commit}) {
dataService.serverSettings()
.then(res => {
console.log(res)
commit('serverSettings', res)
})
.catch(err => {
return err
})
},

},
getters : {
getUsername(state) {
getUsername(state): string {
return state.user != null ? state.user.login : 'anonymous'
},
isAdmin(state) {
isAdmin(state): boolean {
return state.user != null && state.user.isAdmin
},
getSettings(state): ServerSettings {
return state.serverSettings
}
},
plugins : [createLogger()],
Expand Down
4 changes: 3 additions & 1 deletion src/jelu-ui/vuex.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Store } from 'vuex'
import { ServerSettings } from './src/model/ServerSettings';
import { User } from './src/model/User';

declare module '@vue/runtime-core' {
Expand All @@ -8,7 +9,8 @@ interface State {
isLogged: boolean,
isInitialSetup : boolean,
user : User| null,
entryPoint: string
entryPoint: string,
serverSettings: ServerSettings
}

// provide typings for `this.$store`
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/io/github/bayang/jelu/config/JeluProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import javax.validation.constraints.Positive
@ConfigurationProperties(prefix = "jelu")
@ConstructorBinding
@Validated
data class JeluProperties(val database: Database, val files: Files, val session: Session, val cors: Cors = Cors(), val metadata: Metadata) {
data class JeluProperties(
val database: Database,
val files: Files,
val session: Session,
val cors: Cors = Cors(),
val metadata: Metadata = Metadata(Calibre(null))
) {

data class Database(
@get:NotBlank var path: String
Expand All @@ -28,7 +34,7 @@ data class JeluProperties(val database: Database, val files: Files, val session:
)

data class Calibre(
var path: String
var path: String?
)

data class Metadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.bayang.jelu.controllers

import io.github.bayang.jelu.config.JeluProperties
import io.github.bayang.jelu.dto.MetadataDto
import io.github.bayang.jelu.errors.JeluException
import io.github.bayang.jelu.service.metadata.FetchMetadataService
import mu.KotlinLogging
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -24,5 +25,9 @@ class MetadataController(
@RequestParam(name = "title", required = false) title: String?,
@RequestParam(name = "authors", required = false) authors: String?
): MetadataDto =
metadataService.fetchMetadata(isbn, title, authors)
if (properties.metadata.calibre.path.isNullOrBlank()) {
throw JeluException("Automatic fetching of metadata is disabled, install calibre first")
} else {
metadataService.fetchMetadata(isbn, title, authors)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.bayang.jelu.controllers

import io.github.bayang.jelu.config.JeluProperties
import io.github.bayang.jelu.dto.ServerSettingsDto
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api")
class ServerSettingsController(
private val properties: JeluProperties
) {

@GetMapping(path = ["/server-settings"])
fun getServerSettings(): ServerSettingsDto {
return if (properties.metadata.calibre.path.isNullOrEmpty()) {
ServerSettingsDto(metadataFetchEnabled = false, metadataFetchCalibreEnabled = false)
} else {
ServerSettingsDto(metadataFetchEnabled = true, metadataFetchCalibreEnabled = true)
}
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/dto/ServerSettingsDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.bayang.jelu.dto

data class ServerSettingsDto(
/**
* Is there any metadata provider activated at all
*/
val metadataFetchEnabled: Boolean,
/**
* Is the calibre metadata provider activated
*/
val metadataFetchCalibreEnabled: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FetchMetadataService(
throw JeluException("At least one of isbn, authors or title is required to fetch metadata")
}
var bookFileName: String = FILE_PREFIX
val commandArray: MutableList<String> = mutableListOf(properties.metadata.calibre.path, "-o", "-d 90")
val commandArray: MutableList<String> = mutableListOf(properties.metadata.calibre.path!!, "-o", "-d 90")
var fileNameComplete = false
if (!isbn.isNullOrBlank()) {
bookFileName += isbn
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jelu:
metadata:
calibre:
path: /usr/bin/fetch-ebook-metadata
# cors.allowed-origins:
# - http://localhost:9000
cors.allowed-origins:
- http://localhost:3000
server:
port: 11111
spring:
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ jelu:
dir: '${user.home}/.jelu/files/'
session:
duration: 604800 #7 days
metadata:
calibre:
path: /usr/bin/fetch-ebook-metadata
server:
port: 11111
spring:
Expand Down

0 comments on commit c0cec92

Please sign in to comment.