Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load thumbnails for space images instead of the whole file #6529

Merged
merged 16 commits into from
Mar 8, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import UploadImage from '../../../mixins/spaces/actions/uploadImage'
import EditQuota from '../../../mixins/spaces/actions/editQuota'
import QuotaModal from '../../Spaces/QuotaModal.vue'
import ReadmeContentModal from '../../../components/Spaces/ReadmeContentModal.vue'
import { ThumbnailService } from '../../../constants'
import { thumbnailService } from '../../../services'

export default {
name: 'SpaceActions',
Expand All @@ -58,6 +58,9 @@ export default {
UploadImage,
EditQuota
],
data: function () {
AlexAndBear marked this conversation as resolved.
Show resolved Hide resolved
return { thumbnailService }
},
computed: {
...mapGetters('Files', ['highlightedFile']),
resources() {
Expand All @@ -82,9 +85,9 @@ export default {
return this.$data.$_editQuota_modalOpen
},
supportedSpaceImageMimeTypes() {
return ThumbnailService.SupportedMimeTypes.filter((mimeType) =>
mimeType.startsWith('image/')
).join(',')
return thumbnailService.capability.supportedMimeTypes
.filter((mimeType) => mimeType.startsWith('image/'))
.join(',')
}
},
methods: {
Expand Down
11 changes: 0 additions & 11 deletions packages/web-app-files/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,3 @@ export abstract class ImageType {
static readonly Preview: string = 'preview'
static readonly Avatar: string = 'avatar'
}

// FIXME: Load supported mime types of the thumbnail service via capabilities
export abstract class ThumbnailService {
static readonly SupportedMimeTypes: Array<string> = [
'image/png',
'image/jpg',
'image/jpeg',
'image/gif',
'text/plain'
]
}
10 changes: 9 additions & 1 deletion packages/web-app-files/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import quickActions from './quickActions'
import store from './store'
import { FilterSearch, SDKSearch } from './search'
import { bus } from 'web-pkg/src/instance'
import { archiverService, Registry } from './services'
import { archiverService, thumbnailService, Registry } from './services'
import fileSideBars from './fileSideBars'
import { buildRoutes } from './router'
import get from 'lodash-es/get'
Expand Down Expand Up @@ -138,5 +138,13 @@ export default {
store.getters.configuration.server || window.location.origin,
get(store, 'getters.capabilities.files.archivers', [])
)
// FIXME: Remove mock data
thumbnailService.initialize(
get(store, 'getters.capabilities.files.thumbnail', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If guess we'll name it differently (like preview or something more generic), but that's not an issue for now. The way you implemented it allows a fix with minimal footprint ❤️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also check in the future if we can add supported mime types to the capabilities of oc10 as soon as the json schema for the capability is fixed.

enabled: true,
version: 'v0.1',
supportedMimeTypes: ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'text/plain']
})
)
}
}
20 changes: 15 additions & 5 deletions packages/web-app-files/src/mixins/spaces/actions/setImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { clientService } from 'web-pkg/src/services'
import { mapMutations, mapActions, mapGetters } from 'vuex'
import { buildResource } from '../../../helpers/resources'
import { bus } from 'web-pkg/src/instance'
import { ThumbnailService } from '../../../constants'
import { thumbnailService } from '../../../services'

export default {
data: function () {
return {
$_setSpaceImage_thumbnailService: thumbnailService
}
},
computed: {
...mapGetters(['configuration']),
$_setSpaceImage_items() {
Expand All @@ -21,7 +26,15 @@ export default {
if (resources.length !== 1) {
return false
}
if (!this.$_setSpaceImage_supportedMimeTypes.includes(resources[0].mimeType)) {
if (!resources[0].mimeType) {
return false
}
if (
!this.$data.$_setSpaceImage_thumbnailService.isMimetypeSupported(
AlexAndBear marked this conversation as resolved.
Show resolved Hide resolved
resources[0].mimeType,
true
)
) {
return false
}
return isLocationSpacesActive(this.$router, 'files-spaces-project')
Expand All @@ -31,9 +44,6 @@ export default {
class: 'oc-files-actions-set-space-image-trigger'
}
]
},
$_setSpaceImage_supportedMimeTypes: function () {
return ThumbnailService.SupportedMimeTypes.filter((mimeType) => mimeType.startsWith('image/'))
}
},
methods: {
Expand Down
11 changes: 4 additions & 7 deletions packages/web-app-files/src/mixins/spaces/actions/uploadImage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex'
import { clientService } from 'web-pkg/src/services'
import { bus } from 'web-pkg/src/instance'
import { ThumbnailService } from '../../../constants'
import { thumbnailService } from '../../../services'

export default {
data: function () {
return {
$_uploadImage_selectedSpace: null
$_uploadImage_selectedSpace: null,
$_uploadImage_thumbnailService: thumbnailService
}
},
computed: {
Expand Down Expand Up @@ -43,11 +44,7 @@ export default {
const graphClient = clientService.graphAuthenticated(this.configuration.server, this.getToken)
const file = ev.currentTarget.files[0]

if (
!ThumbnailService.SupportedMimeTypes.filter((mimeType) =>
mimeType.startsWith('image/')
).includes(file.type)
) {
if (!this.$data.$_uploadImage_thumbnailService.isMimetypeSupported(file.type, true)) {
return this.showMessage({
title: this.$gettext('The file type is unsupported'),
status: 'danger'
Expand Down
1 change: 1 addition & 0 deletions packages/web-app-files/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './archiver'
export * from './thumbnail'
export * from './cache'
export { default as Registry } from './registry'
26 changes: 26 additions & 0 deletions packages/web-app-files/src/services/thumbnail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
interface ThumbnailCapability {
enabled: boolean
version: string // version is just a major version, e.g. `v2`
supportedMimeTypes: string[]
}

export class ThumbnailService {
serverUrl: string
capability?: ThumbnailCapability

public initialize(thumbnailCapability: ThumbnailCapability): void {
this.capability = thumbnailCapability
}

public get available(): boolean {
return !!this.capability?.version
}

public isMimetypeSupported(mimeType: string, onlyImages = false) {
return onlyImages
? mimeType.startsWith('image/') && this.capability.supportedMimeTypes.includes(mimeType)
: this.capability.supportedMimeTypes.includes(mimeType)
}
}

export const thumbnailService = new ThumbnailService()
14 changes: 8 additions & 6 deletions packages/web-app-files/src/views/spaces/Project.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
name="file"
multiple
tabindex="-1"
accept="image/*"
:accept="supportedSpaceImageMimeTypes"
@change="$_uploadImage_uploadImageSpace"
/>
<ul class="oc-list oc-files-context-actions">
Expand Down Expand Up @@ -160,7 +160,7 @@ import ListInfo from '../../components/FilesList/ListInfo.vue'
import Pagination from '../../components/FilesList/Pagination.vue'
import ContextActions from '../../components/FilesList/ContextActions.vue'
import MixinFileActions from '../../mixins/fileActions'
import { ImageDimension, ImageType, ThumbnailService } from '../../constants'
import { ImageDimension, ImageType } from '../../constants'
import debounce from 'lodash-es/debounce'
import { VisibilityObserver } from 'web-pkg/src/observer'
import { clientService } from 'web-pkg/src/services'
Expand All @@ -176,6 +176,7 @@ import EditQuota from '../../mixins/spaces/actions/editQuota'
import EditReadmeContent from '../../mixins/spaces/actions/editReadmeContent'
import QuotaModal from '../../components/Spaces/QuotaModal.vue'
import ReadmeContentModal from '../../components/Spaces/ReadmeContentModal.vue'
import { thumbnailService } from '../../services'

const visibilityObserver = new VisibilityObserver()

Expand Down Expand Up @@ -303,7 +304,8 @@ export default {
showMarkdownCollapse: false,
markdownResizeObserver: new ResizeObserver(this.onMarkdownResize),
imageExpanded: false,
imageContent: null
imageContent: null,
thumbnailService
}
},
computed: {
Expand Down Expand Up @@ -361,9 +363,9 @@ export default {
return this.$data.$_editReadmeContent_modalOpen
},
supportedSpaceImageMimeTypes() {
return ThumbnailService.SupportedMimeTypes.filter((mimeType) =>
mimeType.startsWith('image/')
).join(',')
return thumbnailService.capability.supportedMimeTypes
AlexAndBear marked this conversation as resolved.
Show resolved Hide resolved
.filter((mimeType) => mimeType.startsWith('image/'))
.join(',')
}
},
watch: {
Expand Down