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
1501522
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php

namespace App\Services;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;

class FreepikStockAssetService
{
    public function importForEdit(string $kind, int $assetId): array
    {
        $normalizedKind = $this->normalizeKind($kind);

        return match ($normalizedKind) {
            'images' => $this->importImagePreview($assetId),
            'videos' => $this->importVideoPreview($assetId),
            'icons' => $this->importIconPng($assetId),
            default => throw new RuntimeException('Asset jenis ini belum didukung untuk AI edit di dalam project.'),
        };
    }

    public function downloadToProject(string $kind, int $assetId): array
    {
        $normalizedKind = $this->normalizeKind($kind);

        return match ($normalizedKind) {
            'images' => $this->downloadImageAsset($assetId),
            'videos' => $this->downloadVideoAsset($assetId),
            'icons' => $this->downloadIconAsset($assetId),
            'audio' => $this->downloadMusicAsset($assetId),
            default => throw new RuntimeException('Jenis asset Freepik tidak valid untuk didownload.'),
        };
    }

    private function importImagePreview(int $assetId): array
    {
        $detail = $this->detail('images', $assetId);
        $sourceUrl = trim((string) data_get($detail, 'image.source.url', ''));

        if ($sourceUrl === '') {
            $download = $this->downloadImageAsset($assetId);

            return [
                'type' => 'image',
                'provider' => 'freepik-stock',
                'title' => (string) ($download['title'] ?? 'Freepik image'),
                'url' => $download['public_url'],
                'thumbnail_url' => $download['public_url'],
                'width' => null,
                'height' => null,
                'kind' => 'images',
                'remote_id' => $assetId,
            ];
        }

        $stored = $this->storeRemoteFile($sourceUrl, [
            'folder' => 'freepik_stock/imports/images',
            'filename' => $this->slugFilename((string) data_get($detail, 'title', 'freepik-image'), $sourceUrl),
        ]);

        return [
            'type' => 'image',
            'provider' => 'freepik-stock',
            'title' => (string) data_get($detail, 'title', 'Freepik image'),
            'url' => $stored['public_url'],
            'thumbnail_url' => $stored['public_url'],
            'width' => $this->numericOrNull(data_get($detail, 'image.source.width')),
            'height' => $this->numericOrNull(data_get($detail, 'image.source.height')),
            'kind' => 'images',
            'remote_id' => $assetId,
        ];
    }

    private function importVideoPreview(int $assetId): array
    {
        $detail = $this->detail('videos', $assetId);
        $previewUrl = trim((string) data_get($detail, 'previews.0.url', ''));
        $thumbnailUrl = trim((string) data_get($detail, 'thumbnails.0.url', ''));

        if ($previewUrl === '') {
            throw new RuntimeException('Freepik video preview tidak tersedia untuk AI edit.');
        }

        $stored = $this->storeRemoteFile($previewUrl, [
            'folder' => 'freepik_stock/imports/videos',
            'filename' => $this->slugFilename((string) data_get($detail, 'name', 'freepik-video'), $previewUrl),
        ]);

        if ($thumbnailUrl !== '') {
            try {
                $thumbnailStored = $this->storeRemoteFile($thumbnailUrl, [
                    'folder' => 'freepik_stock/imports/video_thumbs',
                    'filename' => $this->slugFilename((string) data_get($detail, 'name', 'freepik-video-thumb'), $thumbnailUrl),
                ]);
                $thumbnailUrl = $thumbnailStored['public_url'];
            } catch (\Throwable) {
                // Keep the original remote thumbnail if the local copy fails.
            }
        }

        return [
            'type' => 'video',
            'provider' => 'freepik-stock',
            'title' => (string) data_get($detail, 'name', 'Freepik video'),
            'url' => $stored['public_url'],
            'thumbnail_url' => $thumbnailUrl,
            'width' => $this->numericOrNull(data_get($detail, 'thumbnails.0.width')),
            'height' => $this->numericOrNull(data_get($detail, 'thumbnails.0.height')),
            'duration_seconds' => $this->durationToSeconds((string) data_get($detail, 'duration', '')),
            'kind' => 'videos',
            'remote_id' => $assetId,
        ];
    }

    private function importIconPng(int $assetId): array
    {
        $download = $this->downloadIconAsset($assetId);

        return [
            'type' => 'image',
            'provider' => 'freepik-stock',
            'title' => (string) ($download['title'] ?? 'Freepik icon'),
            'url' => $download['public_url'],
            'thumbnail_url' => $download['public_url'],
            'width' => 512,
            'height' => 512,
            'kind' => 'icons',
            'remote_id' => $assetId,
        ];
    }

    private function downloadImageAsset(int $assetId): array
    {
        $detail = $this->detail('images', $assetId);
        $download = $this->authorizedGet($this->endpoint("/v1/resources/{$assetId}/download"), [
            'image_size' => 'original',
        ], 'Freepik resource download gagal');

        $data = $this->extractData($download, 'Freepik resource download gagal');
        $remoteUrl = trim((string) data_get($data, 'signed_url', data_get($data, 'url', '')));

        if ($remoteUrl === '') {
            throw new RuntimeException('Freepik resource download tidak mengembalikan URL file.');
        }

        $stored = $this->storeRemoteFile($remoteUrl, [
            'folder' => 'freepik_stock/downloads/images',
            'filename' => (string) data_get($data, 'filename', $this->slugFilename((string) data_get($detail, 'title', 'freepik-image'), $remoteUrl)),
        ]);

        return [
            ...$stored,
            'title' => (string) data_get($detail, 'title', 'Freepik image'),
            'kind' => 'images',
            'remote_id' => $assetId,
        ];
    }

    private function downloadVideoAsset(int $assetId): array
    {
        $detail = $this->detail('videos', $assetId);
        $download = $this->authorizedGet($this->endpoint("/v1/videos/{$assetId}/download"), [], 'Freepik video download gagal');
        $data = $this->extractData($download, 'Freepik video download gagal');
        $remoteUrl = trim((string) data_get($data, 'url', ''));

        if ($remoteUrl === '') {
            throw new RuntimeException('Freepik video download tidak mengembalikan URL file.');
        }

        $stored = $this->storeRemoteFile($remoteUrl, [
            'folder' => 'freepik_stock/downloads/videos',
            'filename' => (string) data_get($data, 'filename', $this->slugFilename((string) data_get($detail, 'name', 'freepik-video'), $remoteUrl)),
        ]);

        return [
            ...$stored,
            'title' => (string) data_get($detail, 'name', 'Freepik video'),
            'kind' => 'videos',
            'remote_id' => $assetId,
        ];
    }

    private function downloadIconAsset(int $assetId): array
    {
        $detail = $this->detail('icons', $assetId);
        $download = $this->authorizedGet($this->endpoint("/v1/icons/{$assetId}/download"), [
            'format' => 'png',
            'png_size' => 512,
        ], 'Freepik icon download gagal');
        $data = $this->extractData($download, 'Freepik icon download gagal');
        $remoteUrl = trim((string) data_get($data, 'url', ''));

        if ($remoteUrl === '') {
            throw new RuntimeException('Freepik icon download tidak mengembalikan URL file.');
        }

        $stored = $this->storeRemoteFile($remoteUrl, [
            'folder' => 'freepik_stock/downloads/icons',
            'filename' => (string) data_get($data, 'filename', $this->slugFilename((string) data_get($detail, 'name', 'freepik-icon'), $remoteUrl)),
        ]);

        return [
            ...$stored,
            'title' => (string) data_get($detail, 'name', 'Freepik icon'),
            'kind' => 'icons',
            'remote_id' => $assetId,
        ];
    }

    private function downloadMusicAsset(int $assetId): array
    {
        $detail = $this->detail('audio', $assetId);
        $remoteUrl = trim((string) data_get($detail, 'download_url', data_get($detail, 'file_url', '')));

        if ($remoteUrl === '') {
            throw new RuntimeException('Freepik music download URL tidak tersedia.');
        }

        $stored = $this->storeRemoteFile($remoteUrl, [
            'folder' => 'freepik_stock/downloads/audio',
            'filename' => $this->slugFilename((string) data_get($detail, 'title', 'freepik-audio'), $remoteUrl),
        ]);

        return [
            ...$stored,
            'title' => (string) data_get($detail, 'title', 'Freepik audio'),
            'kind' => 'audio',
            'remote_id' => $assetId,
        ];
    }

    private function detail(string $kind, int $assetId): array
    {
        return match ($this->normalizeKind($kind)) {
            'images' => $this->extractData(
                $this->authorizedGet($this->endpoint("/v1/resources/{$assetId}"), [], 'Freepik resource detail gagal'),
                'Freepik resource detail gagal'
            ),
            'videos' => $this->extractData(
                $this->authorizedGet($this->endpoint("/v1/videos/{$assetId}"), [], 'Freepik video detail gagal'),
                'Freepik video detail gagal'
            ),
            'icons' => $this->extractData(
                $this->authorizedGet($this->endpoint("/v1/icons/{$assetId}"), [], 'Freepik icon detail gagal'),
                'Freepik icon detail gagal'
            ),
            'audio' => $this->extractRawOrData(
                $this->authorizedGet($this->endpoint("/v1/music/{$assetId}"), [], 'Freepik music detail gagal'),
                'Freepik music detail gagal'
            ),
            default => throw new RuntimeException('Jenis asset tidak didukung.'),
        };
    }

    private function authorizedGet(string $url, array $query, string $prefix): Response
    {
        try {
            $response = $this->client()->get($url, $query);
        } catch (ConnectionException $e) {
            throw new RuntimeException($prefix.': koneksi ke Freepik sempat terputus (network reset/TLS). Coba ulangi beberapa detik lagi.');
        }

        if (! $response->successful()) {
            throw new RuntimeException($prefix.': '.$response->status().' - '.$this->responseError($response));
        }

        return $response;
    }

    private function client()
    {
        $apiKey = trim((string) config('services.freepik.api_key', ''));

        if ($apiKey === '') {
            throw new RuntimeException($this->missingApiKeyMessage());
        }

        return Http::connectTimeout(20)
            ->timeout(90)
            ->retry(3, 600, fn ($exception) => $exception instanceof ConnectionException, throw: false)
            ->acceptJson()
            ->withOptions([
                'force_ip_resolve' => 'v4',
            ])
            ->withHeaders([
                'x-freepik-api-key' => $apiKey,
                'User-Agent' => 'TeknoMedia/1.0 (+Laravel Freepik Client)',
            ]);
    }

    private function endpoint(string $path): string
    {
        return rtrim($this->apiOrigin(), '/').$path;
    }

    private function apiOrigin(): string
    {
        $configured = trim((string) config('services.freepik.base_url', 'https://api.freepik.com/v1/ai/text-to-image'));
        $scheme = parse_url($configured, PHP_URL_SCHEME) ?: 'https';
        $host = parse_url($configured, PHP_URL_HOST);
        $port = parse_url($configured, PHP_URL_PORT);

        if (! is_string($host) || $host === '') {
            return 'https://api.freepik.com';
        }

        return $scheme.'://'.$host.($port ? ':'.$port : '');
    }

    private function extractData(Response $response, string $prefix): array
    {
        $json = $response->json();
        $data = data_get($json, 'data', $json);

        if (! is_array($data)) {
            throw new RuntimeException($prefix.': format response Freepik tidak dikenali.');
        }

        return $data;
    }

    private function extractRawOrData(Response $response, string $prefix): array
    {
        $json = $response->json();
        $data = data_get($json, 'data', $json);

        if (! is_array($data)) {
            throw new RuntimeException($prefix.': format response Freepik tidak dikenali.');
        }

        return $data;
    }

    private function responseError(Response $response): string
    {
        $message = data_get($response->json(), 'message')
            ?? data_get($response->json(), 'error.message')
            ?? data_get($response->json(), 'detail')
            ?? $response->body();

        return Str::limit(trim((string) $message), 260);
    }

    private function storeRemoteFile(string $remoteUrl, array $options = []): array
    {
        $folder = trim((string) ($options['folder'] ?? 'freepik_stock/downloads'));
        $preferredName = trim((string) ($options['filename'] ?? ''));
        $tempPath = tempnam(sys_get_temp_dir(), 'fp_');

        if ($tempPath === false) {
            throw new RuntimeException('Gagal membuat file sementara untuk download Freepik.');
        }

        try {
            try {
                $response = Http::connectTimeout(20)
                    ->timeout(180)
                    ->retry(3, 600, fn ($exception) => $exception instanceof ConnectionException, throw: false)
                    ->withOptions([
                        'sink' => $tempPath,
                        'allow_redirects' => true,
                        'force_ip_resolve' => 'v4',
                    ])
                    ->withHeaders([
                        'User-Agent' => 'TeknoMedia/1.0 (+Laravel Freepik Client)',
                    ])
                    ->get($remoteUrl);
            } catch (ConnectionException $e) {
                throw new RuntimeException('Koneksi download asset Freepik sempat reset. Coba ulangi sekali lagi.');
            }

            if (! $response->successful()) {
                throw new RuntimeException('Gagal mengambil file dari Freepik download URL: '.$response->status());
            }

            $pathSource = $preferredName !== ''
                ? $preferredName
                : (parse_url($remoteUrl, PHP_URL_PATH) ?: '');
            $extension = strtolower(pathinfo($pathSource, PATHINFO_EXTENSION));
            if ($extension === '') {
                $extension = $this->extensionFromMime((string) $response->header('Content-Type'));
            }

            $baseName = $preferredName !== ''
                ? pathinfo($preferredName, PATHINFO_FILENAME)
                : 'freepik_'.Str::lower(Str::random(10));

            $safeFilename = Str::slug($baseName);
            if ($safeFilename === '') {
                $safeFilename = 'freepik_'.Str::lower(Str::random(10));
            }

            $finalFilename = $safeFilename.($extension !== '' ? '.'.$extension : '');
            $relativePath = trim($folder, '/').'/'.now()->format('Ymd_His').'_'.$finalFilename;

            $stream = fopen($tempPath, 'rb');
            if ($stream === false) {
                throw new RuntimeException('Gagal membaca file sementara Freepik.');
            }

            Storage::disk('public')->writeStream($relativePath, $stream);

            if (is_resource($stream)) {
                fclose($stream);
            }

            return [
                'relative_path' => $relativePath,
                'public_url' => Storage::url($relativePath),
                'url' => Storage::url($relativePath),
                'path' => Storage::disk('public')->path($relativePath),
                'filename' => $finalFilename,
                'mime' => (string) $response->header('Content-Type', 'application/octet-stream'),
            ];
        } finally {
            if (is_file($tempPath)) {
                @unlink($tempPath);
            }
        }
    }

    private function slugFilename(string $title, string $remoteUrl): string
    {
        $baseName = Str::slug($title);
        if ($baseName === '') {
            $baseName = 'freepik_'.Str::lower(Str::random(10));
        }

        $extension = strtolower(pathinfo(parse_url($remoteUrl, PHP_URL_PATH) ?: $remoteUrl, PATHINFO_EXTENSION));

        return $baseName.($extension !== '' ? '.'.$extension : '');
    }

    private function extensionFromMime(string $mime): string
    {
        return match (Str::before($mime, ';')) {
            'image/jpeg' => 'jpg',
            'image/png' => 'png',
            'image/webp' => 'webp',
            'image/svg+xml' => 'svg',
            'video/mp4' => 'mp4',
            'audio/mpeg' => 'mp3',
            'audio/mp3' => 'mp3',
            'audio/wav' => 'wav',
            'application/zip' => 'zip',
            default => '',
        };
    }

    private function normalizeKind(string $kind): string
    {
        return match ($kind) {
            'image', 'images', 'resource', 'resources' => 'images',
            'video', 'videos' => 'videos',
            'icon', 'icons' => 'icons',
            'audio', 'music' => 'audio',
            default => 'unknown',
        };
    }

    private function numericOrNull(mixed $value): ?int
    {
        return is_numeric($value) ? (int) $value : null;
    }

    private function durationToSeconds(string $duration): ?int
    {
        $parts = array_map('intval', explode(':', $duration));

        if (count($parts) === 3) {
            return ($parts[0] * 3600) + ($parts[1] * 60) + $parts[2];
        }

        if (count($parts) === 2) {
            return ($parts[0] * 60) + $parts[1];
        }

        return null;
    }

    private function missingApiKeyMessage(): string
    {
        $value = strtolower(trim((string) (
            config('services.huggingface.hf_only')
            ?? $_ENV['TOOLS_HF_ONLY']
            ?? $_SERVER['TOOLS_HF_ONLY']
            ?? getenv('TOOLS_HF_ONLY')
            ?? 'false'
        )));

        if (in_array($value, ['1', 'true', 'yes', 'on'], true)) {
            return 'Fitur stock import/download dimatikan di mode Hugging Face-only.';
        }

        return 'FREEPIK_API_KEY belum diisi di file .env';
    }
}