Skip to content

Commit 2f0ce31

Browse files
committed
refactor(go-pro-media.service.ts): move url utility
methods to src/utils/url.ts
1 parent 8c26a53 commit 2f0ce31

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

src/app/features/settings/go-pro/go-pro-media-item-detail-on-camera/go-pro-media-item-detail-on-camera.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
LoadingController,
77
ToastController,
88
} from '@ionic/angular';
9+
import { getFileType } from '../../../../../utils/url';
910
import { GoProFile } from '../go-pro-media-file';
1011
import { GoProMediaService } from '../services/go-pro-media.service';
1112
import { GoProWifiService } from '../services/go-pro-wifi.service';
@@ -42,7 +43,7 @@ export class GoProMediaItemDetailOnCameraComponent implements OnInit {
4243
}
4344

4445
ngOnInit() {
45-
this.mediaType = this.goProMediaService.getFileType(this.mediaFile?.url);
46+
this.mediaType = getFileType(this.mediaFile?.url);
4647
}
4748

4849
async uploadToCapture() {

src/app/features/settings/go-pro/services/go-pro-media.service.ts

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import {
99
Plugins,
1010
} from '@capacitor/core';
1111
import { isPlatform } from '@ionic/core';
12+
import {
13+
detectFileTypeFromUrl,
14+
extractFileNameFromGoProUrl,
15+
urlIsImage,
16+
} from '../../../../../utils/url';
1217
import { FILESYSTEM_PLUGIN } from '../../../../shared/capacitor-plugins/capacitor-plugins.module';
1318
import { CaptureService } from '../../../../shared/capture/capture.service';
1419
import { blobToBase64 } from '../../../../utils/encoding/encoding';
@@ -37,32 +42,6 @@ export class GoProMediaService {
3742
private readonly httpClient: HttpClient
3843
) {}
3944

40-
// eslint-disable-next-line class-methods-use-this
41-
getFileType(url?: string): 'unknown' | 'video' | 'image' {
42-
if (url === undefined) {
43-
return 'unknown';
44-
}
45-
if (url.toLowerCase().includes('.mp4')) {
46-
return 'video';
47-
}
48-
if (
49-
url.toLowerCase().includes('.jpg') ||
50-
url.toLowerCase().includes('.jpeg')
51-
) {
52-
return 'image';
53-
}
54-
return 'unknown';
55-
}
56-
57-
// eslint-disable-next-line class-methods-use-this
58-
extractFileNameFromGoProUrl(url: string): string {
59-
// example of GoPro urls
60-
// _________url: http://10.5.5.9:8080/videos/DCIM/100GOPRO/GH010168.MP4
61-
// thumbnailUrl: http://10.5.5.9:8080/gopro/media/thumbnail?path=100GOPRO/GH010168.MP4
62-
return url.split('/').pop() ?? '';
63-
}
64-
65-
// eslint-disable-next-line class-methods-use-this
6645
getThumbnailUrlFrom(url: string): string {
6746
const fileName = url.split('/').pop();
6847
const thumbnailUrl = `${this.goproBaseUrl}/gopro/media/thumbnail?path=100GOPRO/${fileName}`;
@@ -77,7 +56,7 @@ export class GoProMediaService {
7756
}> {
7857
if (!mediaFile) return { isDownloaded: false, isCaptured: false };
7958

80-
const fileName = this.extractFileNameFromGoProUrl(mediaFile.url);
59+
const fileName = extractFileNameFromGoProUrl(mediaFile.url);
8160

8261
let isDownloaded = false;
8362
let isCaptured = false;
@@ -103,9 +82,7 @@ export class GoProMediaService {
10382

10483
const base64 = await blobToBase64(blob);
10584

106-
const mimeType = this.urlIsImage(mediaFile.url)
107-
? 'image/jpeg'
108-
: 'video/mp4';
85+
const mimeType = urlIsImage(mediaFile.url) ? 'image/jpeg' : 'video/mp4';
10986
isDownloaded = true;
11087

11188
await this.captureService.capture({ base64, mimeType });
@@ -125,29 +102,6 @@ export class GoProMediaService {
125102
return { isDownloaded, isCaptured };
126103
}
127104

128-
// eslint-disable-next-line class-methods-use-this
129-
urlIsImage(url: string): boolean {
130-
return (
131-
url.toLocaleLowerCase().includes('.jpeg') ||
132-
url.toLocaleLowerCase().includes('.jpg')
133-
);
134-
}
135-
136-
// eslint-disable-next-line class-methods-use-this
137-
urlIsVideo(url: string): boolean {
138-
return url.toLowerCase().includes('.mp4');
139-
}
140-
141-
detectFileTypeFromUrl(url: string): 'image' | 'video' | 'unknown' {
142-
if (this.urlIsImage(url)) {
143-
return 'image';
144-
}
145-
if (this.urlIsVideo(url)) {
146-
return 'video';
147-
}
148-
return 'unknown';
149-
}
150-
151105
async getFilesFromGoPro(): Promise<GoProFile[]> {
152106
const url = this.goproBaseUrl + '/gopro/media/list';
153107
const params = {};
@@ -173,8 +127,8 @@ export class GoProMediaService {
173127
url,
174128
storageKey: undefined,
175129
thumbnailUrl: this.getThumbnailUrlFrom(url),
176-
name: this.extractFileNameFromGoProUrl(url),
177-
type: this.detectFileTypeFromUrl(url),
130+
name: extractFileNameFromGoProUrl(url),
131+
type: detectFileTypeFromUrl(url),
178132
};
179133
}
180134
}

src/utils/url.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export function getFileType(url?: string): 'unknown' | 'video' | 'image' {
2+
if (url === undefined) {
3+
return 'unknown';
4+
}
5+
if (url.toLowerCase().includes('.mp4')) {
6+
return 'video';
7+
}
8+
if (
9+
url.toLowerCase().includes('.jpg') ||
10+
url.toLowerCase().includes('.jpeg')
11+
) {
12+
return 'image';
13+
}
14+
return 'unknown';
15+
}
16+
17+
/**
18+
* @param url - Exmaple urls from GoPro
19+
* * http://10.5.5.9:8080/videos/DCIM/100GOPRO/GH010168.MP4
20+
* * http://10.5.5.9:8080/gopro/media/thumbnail?path=100GOPRO/GH010168.MP4
21+
*
22+
* @returns fileName from url - For example: GH010168.MP4
23+
*/
24+
export function extractFileNameFromGoProUrl(url: string): string {
25+
return url.split('/').pop() ?? '';
26+
}
27+
28+
export function urlIsImage(url: string): boolean {
29+
return (
30+
url.toLocaleLowerCase().includes('.jpeg') ||
31+
url.toLocaleLowerCase().includes('.jpg')
32+
);
33+
}
34+
35+
export function urlIsVideo(url: string): boolean {
36+
return url.toLowerCase().includes('.mp4');
37+
}
38+
39+
export function detectFileTypeFromUrl(
40+
url: string
41+
): 'image' | 'video' | 'unknown' {
42+
if (urlIsImage(url)) {
43+
return 'image';
44+
}
45+
if (urlIsVideo(url)) {
46+
return 'video';
47+
}
48+
return 'unknown';
49+
}

0 commit comments

Comments
 (0)