-
Notifications
You must be signed in to change notification settings - Fork 268
/
image-utils.ts
138 lines (124 loc) · 4.48 KB
/
image-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**********************************************************************
* Copyright (C) 2022 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type { ImageInfo } from '../../../../main/src/plugin/api/image-info';
import type { ImageInfoUI } from './ImageInfoUI';
import moment from 'moment';
import humanizeDuration from 'humanize-duration';
import { filesize } from 'filesize';
import { Buffer } from 'buffer';
import type { ContainerInfo } from '../../../../main/src/plugin/api/container-info';
export class ImageUtils {
// extract SHA256 from image id and take the first 12 digits
getShortId(id: string): string {
if (id.startsWith('sha256:')) {
id = id.substring('sha256:'.length);
}
return id.substring(0, 12);
}
getHumanSize(size: number): string {
return `${filesize(size)}`;
}
humanizeAge(created: number): string {
// get start time in ms (using unix timestamp for the created)
const age = moment().diff(moment.unix(created));
// make it human friendly
return humanizeDuration(age, { round: true, largest: 1 });
}
refreshAge(imageInfoUi: ImageInfoUI): string {
// make it human friendly
return this.humanizeAge(imageInfoUi.createdAt);
}
getName(repoTag: string) {
const indexTag = repoTag.lastIndexOf(':');
if (indexTag > 0) {
return repoTag.slice(0, indexTag);
} else {
return '';
}
}
getTag(repoTag: string) {
const indexTag = repoTag.lastIndexOf(':');
if (indexTag > 0) {
return repoTag.slice(indexTag + 1);
} else {
return '';
}
}
getEngineId(containerInfo: ImageInfo): string {
return containerInfo.engineId;
}
getEngineName(containerInfo: ImageInfo): string {
return containerInfo.engineName;
}
getBase64EncodedName(name: string) {
return Buffer.from(name, 'binary').toString('base64');
}
// is that this image is used by a container or not
// search if there is a container matching this image
getInUse(imageInfo: ImageInfo, containersInfo?: ContainerInfo[]): boolean {
if (!containersInfo) {
return false;
}
return containersInfo.some(container => container.ImageID === imageInfo.Id);
}
getImagesInfoUI(imageInfo: ImageInfo, containersInfo: ContainerInfo[]): ImageInfoUI[] {
if (!imageInfo.RepoTags) {
return [
{
id: imageInfo.Id,
shortId: this.getShortId(imageInfo.Id),
createdAt: imageInfo.Created,
age: this.humanizeAge(imageInfo.Created),
humanSize: this.getHumanSize(imageInfo.Size),
name: '<none>',
engineId: this.getEngineId(imageInfo),
engineName: this.getEngineName(imageInfo),
tag: '',
base64RepoTag: this.getBase64EncodedName('<none>'),
selected: false,
inUse: this.getInUse(imageInfo, containersInfo),
},
];
} else {
return imageInfo.RepoTags.map(repoTag => {
return {
id: imageInfo.Id,
shortId: this.getShortId(imageInfo.Id),
createdAt: imageInfo.Created,
age: this.humanizeAge(imageInfo.Created),
humanSize: this.getHumanSize(imageInfo.Size),
name: this.getName(repoTag),
engineId: this.getEngineId(imageInfo),
engineName: this.getEngineName(imageInfo),
tag: this.getTag(repoTag),
base64RepoTag: this.getBase64EncodedName(repoTag),
selected: false,
inUse: this.getInUse(imageInfo, containersInfo),
};
});
}
}
deleteImage(image: ImageInfoUI) {
const imageId = image.name === '<none>' ? image.id : `${image.name}:${image.tag}`;
return window.deleteImage(image.engineId, imageId);
}
getImageInfoUI(imageInfo: ImageInfo, base64RepoTag: string): ImageInfoUI | undefined {
const images = this.getImagesInfoUI(imageInfo, []);
return images.find(image => image.base64RepoTag === base64RepoTag);
}
}