@umituz/react-native-design-system 2.8.8 → 2.8.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -1
- package/src/exports/media.ts +1 -0
- package/src/index.ts +5 -0
- package/src/media/domain/entities/CardMultimedia.types.README.md +129 -0
- package/src/media/domain/entities/CardMultimedia.types.ts +105 -0
- package/src/media/domain/entities/Media.README.md +80 -0
- package/src/media/domain/entities/Media.ts +139 -0
- package/src/media/domain/entities/MultimediaFlashcardTypes.README.md +144 -0
- package/src/media/domain/entities/MultimediaFlashcardTypes.ts +105 -0
- package/src/media/domain/utils/MediaUtils.README.md +178 -0
- package/src/media/domain/utils/MediaUtils.ts +82 -0
- package/src/media/index.ts +70 -0
- package/src/media/index.ts.README.md +191 -0
- package/src/media/infrastructure/services/CardMediaGenerationService.README.md +99 -0
- package/src/media/infrastructure/services/CardMediaGenerationService.ts +101 -0
- package/src/media/infrastructure/services/CardMediaOptimizerService.README.md +167 -0
- package/src/media/infrastructure/services/CardMediaOptimizerService.ts +36 -0
- package/src/media/infrastructure/services/CardMediaUploadService.README.md +123 -0
- package/src/media/infrastructure/services/CardMediaUploadService.ts +67 -0
- package/src/media/infrastructure/services/CardMediaValidationService.README.md +134 -0
- package/src/media/infrastructure/services/CardMediaValidationService.ts +81 -0
- package/src/media/infrastructure/services/CardMultimediaService.README.md +176 -0
- package/src/media/infrastructure/services/CardMultimediaService.ts +97 -0
- package/src/media/infrastructure/services/MediaGenerationService.README.md +142 -0
- package/src/media/infrastructure/services/MediaGenerationService.ts +80 -0
- package/src/media/infrastructure/services/MediaOptimizerService.README.md +145 -0
- package/src/media/infrastructure/services/MediaOptimizerService.ts +32 -0
- package/src/media/infrastructure/services/MediaPickerService.README.md +106 -0
- package/src/media/infrastructure/services/MediaPickerService.ts +173 -0
- package/src/media/infrastructure/services/MediaSaveService.README.md +120 -0
- package/src/media/infrastructure/services/MediaSaveService.ts +154 -0
- package/src/media/infrastructure/services/MediaUploadService.README.md +135 -0
- package/src/media/infrastructure/services/MediaUploadService.ts +62 -0
- package/src/media/infrastructure/services/MediaValidationService.README.md +135 -0
- package/src/media/infrastructure/services/MediaValidationService.ts +61 -0
- package/src/media/infrastructure/services/MultimediaFlashcardService.README.md +142 -0
- package/src/media/infrastructure/services/MultimediaFlashcardService.ts +95 -0
- package/src/media/infrastructure/utils/mediaHelpers.README.md +96 -0
- package/src/media/infrastructure/utils/mediaHelpers.ts +82 -0
- package/src/media/infrastructure/utils/mediaPickerMappers.README.md +129 -0
- package/src/media/infrastructure/utils/mediaPickerMappers.ts +76 -0
- package/src/media/presentation/hooks/card-multimedia.types.README.md +177 -0
- package/src/media/presentation/hooks/card-multimedia.types.ts +51 -0
- package/src/media/presentation/hooks/multimedia.types.README.md +201 -0
- package/src/media/presentation/hooks/multimedia.types.ts +51 -0
- package/src/media/presentation/hooks/useCardMediaGeneration.README.md +164 -0
- package/src/media/presentation/hooks/useCardMediaGeneration.ts +124 -0
- package/src/media/presentation/hooks/useCardMediaUpload.README.md +153 -0
- package/src/media/presentation/hooks/useCardMediaUpload.ts +86 -0
- package/src/media/presentation/hooks/useCardMediaValidation.README.md +176 -0
- package/src/media/presentation/hooks/useCardMediaValidation.ts +101 -0
- package/src/media/presentation/hooks/useCardMultimediaFlashcard.README.md +158 -0
- package/src/media/presentation/hooks/useCardMultimediaFlashcard.ts +104 -0
- package/src/media/presentation/hooks/useMedia.README.md +94 -0
- package/src/media/presentation/hooks/useMedia.ts +186 -0
- package/src/media/presentation/hooks/useMediaGeneration.README.md +118 -0
- package/src/media/presentation/hooks/useMediaGeneration.ts +101 -0
- package/src/media/presentation/hooks/useMediaUpload.README.md +108 -0
- package/src/media/presentation/hooks/useMediaUpload.ts +86 -0
- package/src/media/presentation/hooks/useMediaValidation.README.md +134 -0
- package/src/media/presentation/hooks/useMediaValidation.ts +93 -0
- package/src/media/presentation/hooks/useMultimediaFlashcard.README.md +141 -0
- package/src/media/presentation/hooks/useMultimediaFlashcard.ts +103 -0
- package/src/storage/infrastructure/repositories/__tests__/AsyncStorageRepository.test.ts +1 -1
- package/src/storage/infrastructure/repositories/__tests__/BaseStorageOperations.test.ts +1 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# CardMediaGenerationService
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
Service that performs AI-based media generation operations for flashcards, including text-to-image, text-to-audio, and image-search capabilities.
|
|
5
|
+
|
|
6
|
+
## File Location
|
|
7
|
+
`/Users/umituz/Desktop/github/umituz/apps/artificial_intelligence/npm-packages/react-native-media/src/infrastructure/services/CardMediaGenerationService`
|
|
8
|
+
|
|
9
|
+
## Strategy
|
|
10
|
+
- Generate media content using AI APIs for card applications
|
|
11
|
+
- Support multiple generation types (text-to-image, text-to-audio, image-search)
|
|
12
|
+
- Track processing time and credit/balance usage
|
|
13
|
+
- Return card-compatible media attachments with position support
|
|
14
|
+
- Provide detailed result information including success status and error messages
|
|
15
|
+
- Enable batch generation with multiple results
|
|
16
|
+
- Maintain unique request IDs for tracking and debugging
|
|
17
|
+
|
|
18
|
+
## Forbidden
|
|
19
|
+
- **DO NOT** generate media without checking available credits/balance
|
|
20
|
+
- **DO NOT** use empty or null prompts for generation
|
|
21
|
+
- **DO NOT** assume all generation requests will succeed
|
|
22
|
+
- **DO NOT** ignore generation timeout limits
|
|
23
|
+
- **DO NOT** generate inappropriate or copyrighted content
|
|
24
|
+
- **DO NOT** proceed without error handling for API failures
|
|
25
|
+
- **DO NOT** mix generation types in single requests
|
|
26
|
+
- **DO NOT** bypass request ID tracking
|
|
27
|
+
|
|
28
|
+
## Rules
|
|
29
|
+
1. All generation operations must include a valid prompt
|
|
30
|
+
2. Text-to-image generation requires prompt and optional style/maxResults
|
|
31
|
+
3. Text-to-audio generation requires prompt, language, and optional voice/speed
|
|
32
|
+
4. Image search requires keyword prompt and optional maxResults
|
|
33
|
+
5. Credit costs vary by type: text-to-image (5), text-to-audio (3), image-search (2)
|
|
34
|
+
6. All generated media must have position attribute set to 'both' by default
|
|
35
|
+
7. Processing time must be tracked and returned in milliseconds
|
|
36
|
+
8. Unique request IDs must be generated for each operation
|
|
37
|
+
9. Generation failures must return clear error messages
|
|
38
|
+
10. Audio files must be assigned a 10-second duration by default
|
|
39
|
+
11. maxResults defaults to 1 for text-to-image, 5 for image-search
|
|
40
|
+
|
|
41
|
+
## AI Agent Guidelines
|
|
42
|
+
|
|
43
|
+
When working with CardMediaGenerationService:
|
|
44
|
+
|
|
45
|
+
1. **Pre-generation Checks**: Always verify credit/balance before generation requests
|
|
46
|
+
2. **Prompt Quality**: Use clear, descriptive prompts for better results
|
|
47
|
+
3. **Type Selection**: Choose appropriate generation type for use case
|
|
48
|
+
4. **Result Validation**: Always check success flag before using results
|
|
49
|
+
5. **Error Handling**: Handle API failures, timeouts, and insufficient credits
|
|
50
|
+
6. **Position Assignment**: Set correct position (front/back/both) for card use
|
|
51
|
+
7. **Request Tracking**: Store request IDs for debugging and support
|
|
52
|
+
|
|
53
|
+
### Generation Type Guidelines
|
|
54
|
+
|
|
55
|
+
- **Text-to-Image**: Use for visual content on cards (front typically)
|
|
56
|
+
- Specify style (realistic, artistic) for better results
|
|
57
|
+
- Set maxResults to 1 unless multiple options needed
|
|
58
|
+
- Images receive position='both' by default
|
|
59
|
+
|
|
60
|
+
- **Text-to-Audio**: Use for pronunciation or audio explanations
|
|
61
|
+
- Specify language code (tr-TR, en-US, etc.)
|
|
62
|
+
- Set voice type (male, female) if applicable
|
|
63
|
+
- Adjust speed (0.5-2.0) for playback preferences
|
|
64
|
+
- Audio receives 10-second duration by default
|
|
65
|
+
|
|
66
|
+
- **Image Search**: Use for finding relevant visuals
|
|
67
|
+
- Use specific keywords for better results
|
|
68
|
+
- Set maxResults for multiple options
|
|
69
|
+
- Credits are cheaper than text-to-image
|
|
70
|
+
|
|
71
|
+
### Credit Management
|
|
72
|
+
|
|
73
|
+
- Calculate total credits before batch operations
|
|
74
|
+
- Text-to-image: 5 credits per request
|
|
75
|
+
- Text-to-audio: 3 credits per request
|
|
76
|
+
- Image search: 2 credits per request
|
|
77
|
+
- Handle insufficient credit errors gracefully
|
|
78
|
+
- Track credits used from generation results
|
|
79
|
+
|
|
80
|
+
### Error Handling Patterns
|
|
81
|
+
|
|
82
|
+
- Check success flag before accessing attachments
|
|
83
|
+
- Parse error messages for specific failure types:
|
|
84
|
+
- "insufficient credits" - balance too low
|
|
85
|
+
- "timeout" - operation took too long
|
|
86
|
+
- "invalid prompt" - prompt validation failed
|
|
87
|
+
- "api error" - underlying API failure
|
|
88
|
+
|
|
89
|
+
### Performance Considerations
|
|
90
|
+
|
|
91
|
+
- Generation operations are simulated (3 seconds delay)
|
|
92
|
+
- Batch multiple results in single request when possible
|
|
93
|
+
- Track processing time for performance monitoring
|
|
94
|
+
- Use request IDs for debugging slow operations
|
|
95
|
+
|
|
96
|
+
## Dependencies
|
|
97
|
+
- CardMediaAttachment type from domain layer
|
|
98
|
+
- AI generation APIs (simulated in development)
|
|
99
|
+
- Credit/balance management system
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Card Media Generation Service
|
|
3
|
+
* Handles AI media generation (text-to-image, text-to-audio, image-search)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
CardMediaAttachment,
|
|
8
|
+
CardMediaGenerationRequest,
|
|
9
|
+
CardMediaGenerationResult,
|
|
10
|
+
CardMediaType,
|
|
11
|
+
CardMediaPosition,
|
|
12
|
+
} from "../../domain/entities/CardMultimedia.types";
|
|
13
|
+
|
|
14
|
+
export class CardMediaGenerationService {
|
|
15
|
+
/**
|
|
16
|
+
* Generate media from AI (text-to-image, text-to-audio, etc.)
|
|
17
|
+
*/
|
|
18
|
+
async generateMedia(
|
|
19
|
+
request: CardMediaGenerationRequest,
|
|
20
|
+
): Promise<CardMediaGenerationResult> {
|
|
21
|
+
try {
|
|
22
|
+
const startTime = Date.now();
|
|
23
|
+
|
|
24
|
+
// Simulate AI generation
|
|
25
|
+
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
26
|
+
|
|
27
|
+
const attachments: CardMediaAttachment[] = [];
|
|
28
|
+
|
|
29
|
+
switch (request.type) {
|
|
30
|
+
case "text_to_image":
|
|
31
|
+
for (let i = 0; i < (request.options.maxResults || 1); i++) {
|
|
32
|
+
attachments.push({
|
|
33
|
+
id: `ai_img_${Date.now()}_${i}`,
|
|
34
|
+
type: "image" as CardMediaType,
|
|
35
|
+
position: "both" as CardMediaPosition,
|
|
36
|
+
url: `https://picsum.photos/400/300?random=${Date.now() + i}`,
|
|
37
|
+
filename: `ai_generated_${i}.jpg`,
|
|
38
|
+
fileSize: 150000, // 150KB
|
|
39
|
+
mimeType: "image/jpeg",
|
|
40
|
+
isDownloaded: false,
|
|
41
|
+
createdAt: new Date().toISOString(),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
|
|
46
|
+
case "text_to_audio":
|
|
47
|
+
attachments.push({
|
|
48
|
+
id: `ai_audio_${Date.now()}`,
|
|
49
|
+
type: "audio" as CardMediaType,
|
|
50
|
+
position: "back" as CardMediaPosition,
|
|
51
|
+
url: `https://example.com/audio_${Date.now()}.mp3`,
|
|
52
|
+
filename: `ai_generated_${Date.now()}.mp3`,
|
|
53
|
+
fileSize: 80000, // 80KB
|
|
54
|
+
mimeType: "audio/mp3",
|
|
55
|
+
duration: 10, // 10 seconds
|
|
56
|
+
isDownloaded: false,
|
|
57
|
+
createdAt: new Date().toISOString(),
|
|
58
|
+
});
|
|
59
|
+
break;
|
|
60
|
+
|
|
61
|
+
case "image_search":
|
|
62
|
+
for (let i = 0; i < (request.options.maxResults || 5); i++) {
|
|
63
|
+
attachments.push({
|
|
64
|
+
id: `search_img_${Date.now()}_${i}`,
|
|
65
|
+
type: "image" as CardMediaType,
|
|
66
|
+
position: "both" as CardMediaPosition,
|
|
67
|
+
url: `https://picsum.photos/400/300?random=${Date.now() + i}`,
|
|
68
|
+
filename: `search_result_${i}.jpg`,
|
|
69
|
+
fileSize: 120000, // 120KB
|
|
70
|
+
mimeType: "image/jpeg",
|
|
71
|
+
isDownloaded: false,
|
|
72
|
+
createdAt: new Date().toISOString(),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
success: true,
|
|
80
|
+
attachments,
|
|
81
|
+
creditsUsed:
|
|
82
|
+
request.type === "text_to_image"
|
|
83
|
+
? 5
|
|
84
|
+
: request.type === "text_to_audio"
|
|
85
|
+
? 3
|
|
86
|
+
: 2,
|
|
87
|
+
processingTime: Date.now() - startTime,
|
|
88
|
+
requestId: `req_${Date.now()}`,
|
|
89
|
+
};
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return {
|
|
92
|
+
success: false,
|
|
93
|
+
attachments: [],
|
|
94
|
+
creditsUsed: 0,
|
|
95
|
+
processingTime: 0,
|
|
96
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
97
|
+
requestId: "",
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# CardMediaOptimizerService
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
Service that optimizes and compresses flashcard media files to reduce size while maintaining acceptable quality, and manages media deletion operations.
|
|
5
|
+
|
|
6
|
+
## File Location
|
|
7
|
+
`/Users/umituz/Desktop/github/umituz/apps/artificial_intelligence/npm-packages/react-native-media/src/infrastructure/services/CardMediaOptimizerService`
|
|
8
|
+
|
|
9
|
+
## Strategy
|
|
10
|
+
- Optimize media files by reducing file size with quality settings
|
|
11
|
+
- Support dimension resizing (maxWidth, maxHeight)
|
|
12
|
+
- Provide multiple quality levels for different use cases
|
|
13
|
+
- Generate new optimized versions while preserving originals
|
|
14
|
+
- Delete media from server and local storage
|
|
15
|
+
- Return optimized media with updated file size and URL
|
|
16
|
+
- Maintain card media position through optimization
|
|
17
|
+
- Calculate size reduction percentages
|
|
18
|
+
|
|
19
|
+
## Forbidden
|
|
20
|
+
- **DO NOT** optimize below usable quality (quality < 0.3)
|
|
21
|
+
- **DO NOT** modify original media files
|
|
22
|
+
- **DO NOT** delete media without verifying ownership
|
|
23
|
+
- **DO NOT** assume optimization will always succeed
|
|
24
|
+
- **DO NOT** use negative or invalid quality values
|
|
25
|
+
- **DO NOT** set dimensions to zero or negative values
|
|
26
|
+
- **DO NOT** delete media without user confirmation
|
|
27
|
+
- **DO NOT** bypass optimization error handling
|
|
28
|
+
|
|
29
|
+
## Rules
|
|
30
|
+
1. Quality value must be between 0-1 (0 = worst, 1 = best)
|
|
31
|
+
2. Max dimensions must be positive integers if specified
|
|
32
|
+
3. Original media is always preserved, never modified
|
|
33
|
+
4. Optimized media receives new URL with ?optimized=true parameter
|
|
34
|
+
5. File size is approximately calculated as: original * quality
|
|
35
|
+
6. Position attribute is preserved through optimization
|
|
36
|
+
7. All other attachment properties remain unchanged
|
|
37
|
+
8. Optimization must maintain aspect ratio when resizing
|
|
38
|
+
9. Delete operation is permanent and irreversible
|
|
39
|
+
10. Optimized size calculation is simulated (not actual compression)
|
|
40
|
+
11. Quality levels: high (0.9-1.0), medium (0.7-0.8), low (0.5-0.6)
|
|
41
|
+
|
|
42
|
+
## AI Agent Guidelines
|
|
43
|
+
|
|
44
|
+
When working with CardMediaOptimizerService:
|
|
45
|
+
|
|
46
|
+
1. **Quality Selection**: Choose appropriate quality for use case
|
|
47
|
+
2. **Size Calculation**: Estimate size reduction before optimization
|
|
48
|
+
3. **Original Preservation**: Always keep original media intact
|
|
49
|
+
4. **Error Handling**: Handle optimization failures gracefully
|
|
50
|
+
5. **Position Maintenance**: Preserve position through all operations
|
|
51
|
+
6. **Delete Confirmation**: Always verify before deletion
|
|
52
|
+
7. **Batch Operations**: Consider batch optimization for multiple files
|
|
53
|
+
|
|
54
|
+
### Quality Level Guidelines
|
|
55
|
+
|
|
56
|
+
- **High Quality (0.9-1.0)**:
|
|
57
|
+
- Use for: Card front images, important visuals
|
|
58
|
+
- Size reduction: 10-20%
|
|
59
|
+
- When to use: High quality requirements, focal content
|
|
60
|
+
|
|
61
|
+
- **Medium Quality (0.7-0.8)** - RECOMMENDED:
|
|
62
|
+
- Use for: General card media, back images
|
|
63
|
+
- Size reduction: 30-50%
|
|
64
|
+
- When to use: Balance of quality and size, most cases
|
|
65
|
+
|
|
66
|
+
- **Low Quality (0.5-0.6)**:
|
|
67
|
+
- Use for: Thumbnails, previews, non-critical content
|
|
68
|
+
- Size reduction: 50-70%
|
|
69
|
+
- When to use: Previews, placeholders, bandwidth-constrained
|
|
70
|
+
|
|
71
|
+
- **Very Low Quality (0.3-0.4)**:
|
|
72
|
+
- Use for: Low-quality previews only
|
|
73
|
+
- Size reduction: 70-80%
|
|
74
|
+
- When to use: Extreme bandwidth constraints
|
|
75
|
+
|
|
76
|
+
### Dimension Guidelines
|
|
77
|
+
|
|
78
|
+
- **Full Resolution**: No max dimensions specified
|
|
79
|
+
- Use when: Quality is critical, storage is abundant
|
|
80
|
+
|
|
81
|
+
- **1920x1080** - RECOMMENDED:
|
|
82
|
+
- Use for: General card media, good balance
|
|
83
|
+
- Suitable for: Most modern displays
|
|
84
|
+
|
|
85
|
+
- **1280x720**:
|
|
86
|
+
- Use for: Smaller cards, bandwidth optimization
|
|
87
|
+
- Suitable for: Mobile devices, previews
|
|
88
|
+
|
|
89
|
+
- **Lower than 1280x720**:
|
|
90
|
+
- Use for: Thumbnails, very small cards
|
|
91
|
+
- Consider: May appear pixelated on large screens
|
|
92
|
+
|
|
93
|
+
### Card-Specific Optimization
|
|
94
|
+
|
|
95
|
+
- **Front Side Media**:
|
|
96
|
+
- Use higher quality (0.8-0.9)
|
|
97
|
+
- Full or near-full resolution
|
|
98
|
+
- This is the focal point of the card
|
|
99
|
+
|
|
100
|
+
- **Back Side Media**:
|
|
101
|
+
- Use medium quality (0.7)
|
|
102
|
+
- Moderate resolution (1280x720 or 1920x1080)
|
|
103
|
+
- Secondary content can be more compressed
|
|
104
|
+
|
|
105
|
+
- **Audio Files**:
|
|
106
|
+
- This service is designed for image optimization only
|
|
107
|
+
- Audio optimization requires different approach (bitrate)
|
|
108
|
+
|
|
109
|
+
### Deletion Guidelines
|
|
110
|
+
|
|
111
|
+
- **Before Delete**:
|
|
112
|
+
- Verify media ownership
|
|
113
|
+
- Check card associations
|
|
114
|
+
- Confirm with user if applicable
|
|
115
|
+
- Consider soft delete option
|
|
116
|
+
|
|
117
|
+
- **During Delete**:
|
|
118
|
+
- Remove from server storage
|
|
119
|
+
- Remove from local storage
|
|
120
|
+
- Update card references
|
|
121
|
+
- Handle missing files gracefully
|
|
122
|
+
|
|
123
|
+
- **After Delete**:
|
|
124
|
+
- Update database references
|
|
125
|
+
- Clean up orphaned files
|
|
126
|
+
- Cannot be undone (permanent)
|
|
127
|
+
|
|
128
|
+
### Optimization Workflow
|
|
129
|
+
|
|
130
|
+
1. Start with original media attachment
|
|
131
|
+
2. Determine target quality and dimensions
|
|
132
|
+
3. Calculate expected size reduction
|
|
133
|
+
4. Run optimization operation
|
|
134
|
+
5. Receive optimized attachment with new URL
|
|
135
|
+
6. Replace or keep both versions
|
|
136
|
+
7. Update card media references
|
|
137
|
+
|
|
138
|
+
### Error Handling
|
|
139
|
+
|
|
140
|
+
- **Optimization Failures**:
|
|
141
|
+
- Return original media unchanged
|
|
142
|
+
- Log error for debugging
|
|
143
|
+
- Continue with original if appropriate
|
|
144
|
+
|
|
145
|
+
- **Invalid Parameters**:
|
|
146
|
+
- Quality outside 0-1 range
|
|
147
|
+
- Negative or zero dimensions
|
|
148
|
+
- Must reject with clear error
|
|
149
|
+
|
|
150
|
+
- **Delete Failures**:
|
|
151
|
+
- Media not found
|
|
152
|
+
- Permission denied
|
|
153
|
+
- Handle gracefully, log error
|
|
154
|
+
|
|
155
|
+
### Performance Considerations
|
|
156
|
+
|
|
157
|
+
- Optimization is simulated (size * quality calculation)
|
|
158
|
+
- In production, integrate actual optimization API
|
|
159
|
+
- Consider lazy optimization (optimize on demand)
|
|
160
|
+
- Batch optimization for multiple files
|
|
161
|
+
- Cache optimized versions when possible
|
|
162
|
+
|
|
163
|
+
## Dependencies
|
|
164
|
+
- CardMediaAttachment type from domain layer
|
|
165
|
+
- CardMediaCompressionOptions type for optimization settings
|
|
166
|
+
- Optimization API endpoints (simulated in development)
|
|
167
|
+
- Storage access for deletion operations
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Card Media Optimizer Service
|
|
3
|
+
* Handles media optimization and deletion
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
declare var __DEV__: boolean;
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
CardMediaAttachment,
|
|
10
|
+
CardMediaCompressionOptions,
|
|
11
|
+
} from "../../domain/entities/CardMultimedia.types";
|
|
12
|
+
|
|
13
|
+
export class CardMediaOptimizerService {
|
|
14
|
+
/**
|
|
15
|
+
* Optimize media file
|
|
16
|
+
*/
|
|
17
|
+
async optimizeMedia(
|
|
18
|
+
attachment: CardMediaAttachment,
|
|
19
|
+
options: CardMediaCompressionOptions,
|
|
20
|
+
): Promise<CardMediaAttachment> {
|
|
21
|
+
return {
|
|
22
|
+
...attachment,
|
|
23
|
+
fileSize: Math.floor(attachment.fileSize * options.quality),
|
|
24
|
+
url: `${attachment.url}?optimized=true`,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Delete media attachment
|
|
30
|
+
*/
|
|
31
|
+
async deleteMedia(attachmentId: string): Promise<void> {
|
|
32
|
+
if (__DEV__) {
|
|
33
|
+
console.log(`[CardMediaOptimizerService] Deleting media: ${attachmentId}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# CardMediaUploadService
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
Service that handles media file upload, download, and URL management for flashcard applications, with support for compression and automatic thumbnail generation.
|
|
5
|
+
|
|
6
|
+
## File Location
|
|
7
|
+
`/Users/umituz/Desktop/github/umituz/apps/artificial_intelligence/npm-packages/react-native-media/src/infrastructure/services/CardMediaUploadService`
|
|
8
|
+
|
|
9
|
+
## Strategy
|
|
10
|
+
- Upload media files to server storage with unique identifiers
|
|
11
|
+
- Support compression options during upload (quality, dimensions, format)
|
|
12
|
+
- Generate thumbnails automatically for uploaded media
|
|
13
|
+
- Download media from server to local storage
|
|
14
|
+
- Retrieve and manage media URLs
|
|
15
|
+
- Calculate duration for audio/video files
|
|
16
|
+
- Return card-compatible media attachments with position support
|
|
17
|
+
- Handle all media types (image, video, audio) with automatic type detection
|
|
18
|
+
|
|
19
|
+
## Forbidden
|
|
20
|
+
- **DO NOT** upload files without validation
|
|
21
|
+
- **DO NOT** bypass file size limits
|
|
22
|
+
- **DO NOT** upload unsupported file types
|
|
23
|
+
- **DO NOT** ignore upload failures
|
|
24
|
+
- **DO NOT** assume all uploads will succeed
|
|
25
|
+
- **DO NOT** overwrite existing media without verification
|
|
26
|
+
- **DO NOT** proceed without error handling for network issues
|
|
27
|
+
- **DO NOT** download media without checking URL validity
|
|
28
|
+
|
|
29
|
+
## Rules
|
|
30
|
+
1. All uploads must include valid file metadata (name, type, size, uri)
|
|
31
|
+
2. Compression options are optional but recommended for large files
|
|
32
|
+
3. Quality must be between 0-1 when specified
|
|
33
|
+
4. Max dimensions must be positive integers when specified
|
|
34
|
+
5. Supported formats are jpeg and png for compression
|
|
35
|
+
6. All attachments receive unique IDs automatically
|
|
36
|
+
7. Position defaults to 'both' for uploaded media
|
|
37
|
+
8. isDownloaded defaults to true for uploads
|
|
38
|
+
9. Thumbnails are generated automatically for visual content
|
|
39
|
+
10. Audio/video duration is calculated automatically
|
|
40
|
+
11. File type is determined automatically from MIME type
|
|
41
|
+
12. Upload operations must handle network failures gracefully
|
|
42
|
+
13. Download operations must validate URL before processing
|
|
43
|
+
|
|
44
|
+
## AI Agent Guidelines
|
|
45
|
+
|
|
46
|
+
When working with CardMediaUploadService:
|
|
47
|
+
|
|
48
|
+
1. **Pre-upload Validation**: Always validate files before upload attempts
|
|
49
|
+
2. **Compression Strategy**: Apply compression for files larger than 5MB
|
|
50
|
+
3. **Type Detection**: Rely on automatic type detection from MIME type
|
|
51
|
+
4. **Error Handling**: Handle network timeouts and connection failures
|
|
52
|
+
5. **Position Assignment**: Set correct position (front/back/both) for card use
|
|
53
|
+
6. **Thumbnail Usage**: Use thumbnails for previews to save bandwidth
|
|
54
|
+
7. **Duration Handling**: Use calculated duration for audio/video scheduling
|
|
55
|
+
|
|
56
|
+
### Upload Guidelines
|
|
57
|
+
|
|
58
|
+
- **Before Upload**:
|
|
59
|
+
- Validate file size and type
|
|
60
|
+
- Consider compression for large files
|
|
61
|
+
- Check network connectivity
|
|
62
|
+
- Prepare error handling
|
|
63
|
+
|
|
64
|
+
- **During Upload**:
|
|
65
|
+
- Monitor for failures
|
|
66
|
+
- Handle network errors
|
|
67
|
+
- Store attachment information
|
|
68
|
+
- Track upload progress
|
|
69
|
+
|
|
70
|
+
- **After Upload**:
|
|
71
|
+
- Verify upload success
|
|
72
|
+
- Store attachment ID and URL
|
|
73
|
+
- Set position for card use
|
|
74
|
+
- Consider optimization if needed
|
|
75
|
+
|
|
76
|
+
### Compression Options
|
|
77
|
+
|
|
78
|
+
- **Quality**: 0.9 (high), 0.7 (medium/recommended), 0.5 (low)
|
|
79
|
+
- **Max Dimensions**: 1920x1080 (recommended), 1280x720 (smaller)
|
|
80
|
+
- **Format**: jpeg (smaller size), png (better quality)
|
|
81
|
+
- Apply compression when file size > 5MB for images
|
|
82
|
+
- Use quality 0.7 for balance of size and quality
|
|
83
|
+
|
|
84
|
+
### Media Type Detection
|
|
85
|
+
|
|
86
|
+
- **Image**: MIME types starting with 'image/'
|
|
87
|
+
- Supported: image/jpeg, image/png, image/webp
|
|
88
|
+
- **Audio**: MIME types starting with 'audio/'
|
|
89
|
+
- Supported: audio/mp3, audio/wav, audio/m4a
|
|
90
|
+
- **Video**: MIME types starting with 'video/'
|
|
91
|
+
- Supported: video/mp4, video/mov
|
|
92
|
+
|
|
93
|
+
### Download Guidelines
|
|
94
|
+
|
|
95
|
+
- Validate media URL before download
|
|
96
|
+
- Handle missing files gracefully
|
|
97
|
+
- Use local path for offline access
|
|
98
|
+
- Check isDownloaded status before re-downloading
|
|
99
|
+
- Manage local storage space
|
|
100
|
+
|
|
101
|
+
### Error Handling Patterns
|
|
102
|
+
|
|
103
|
+
- Handle network errors: timeout, connection refused
|
|
104
|
+
- Handle file errors: invalid format, corrupted file
|
|
105
|
+
- Handle server errors: 500, 503, insufficient storage
|
|
106
|
+
- Handle validation errors: size exceeded, unsupported type
|
|
107
|
+
- Always provide fallback behavior for failed uploads
|
|
108
|
+
|
|
109
|
+
### Performance Considerations
|
|
110
|
+
|
|
111
|
+
- Upload operations are simulated (2 seconds delay)
|
|
112
|
+
- Compress before upload to reduce transfer time
|
|
113
|
+
- Use thumbnails for previews instead of full media
|
|
114
|
+
- Batch uploads when possible for multiple files
|
|
115
|
+
- Consider lazy loading for card media
|
|
116
|
+
|
|
117
|
+
## Dependencies
|
|
118
|
+
- CardMediaAttachment type from domain layer
|
|
119
|
+
- CardMediaCompressionOptions type for compression settings
|
|
120
|
+
- getCardMediaType helper for type detection
|
|
121
|
+
- getMediaDuration helper for duration calculation
|
|
122
|
+
- generateThumbnail helper for thumbnail creation
|
|
123
|
+
- Upload API endpoints (simulated in development)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Card Media Upload Service
|
|
3
|
+
* Handles media upload, download, and URL operations
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
declare var __DEV__: boolean;
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
CardMediaAttachment,
|
|
10
|
+
CardMediaCompressionOptions,
|
|
11
|
+
} from "../../domain/entities/CardMultimedia.types";
|
|
12
|
+
import {
|
|
13
|
+
generateThumbnail,
|
|
14
|
+
getCardMediaType,
|
|
15
|
+
getMediaDuration,
|
|
16
|
+
} from "../utils/mediaHelpers";
|
|
17
|
+
|
|
18
|
+
export class CardMediaUploadService {
|
|
19
|
+
/**
|
|
20
|
+
* Upload media file with optional compression
|
|
21
|
+
*/
|
|
22
|
+
async uploadMedia(
|
|
23
|
+
file: any,
|
|
24
|
+
_options?: CardMediaCompressionOptions,
|
|
25
|
+
): Promise<CardMediaAttachment> {
|
|
26
|
+
try {
|
|
27
|
+
// Simulate upload process
|
|
28
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
29
|
+
|
|
30
|
+
const attachment: CardMediaAttachment = {
|
|
31
|
+
id: `card_media_${Date.now()}`,
|
|
32
|
+
type: getCardMediaType(file.type),
|
|
33
|
+
position: "both",
|
|
34
|
+
url: `https://storage.example.com/media/${Date.now()}_${file.name}`,
|
|
35
|
+
filename: file.name,
|
|
36
|
+
fileSize: file.size || 100000,
|
|
37
|
+
mimeType: file.type,
|
|
38
|
+
duration: await getMediaDuration(file),
|
|
39
|
+
thumbnailUrl: generateThumbnail(file),
|
|
40
|
+
caption: "",
|
|
41
|
+
isDownloaded: true,
|
|
42
|
+
createdAt: new Date().toISOString(),
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return attachment;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw new Error(`Failed to upload media: ${error}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get media URL
|
|
53
|
+
*/
|
|
54
|
+
async getMediaUrl(attachmentId: string): Promise<string> {
|
|
55
|
+
return `https://storage.example.com/media/${attachmentId}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Download media to local storage
|
|
60
|
+
*/
|
|
61
|
+
async downloadMedia(attachmentId: string): Promise<string> {
|
|
62
|
+
if (__DEV__) {
|
|
63
|
+
console.log(`[CardMediaUploadService] Downloading media: ${attachmentId}`);
|
|
64
|
+
}
|
|
65
|
+
return `/local/storage/${attachmentId}`;
|
|
66
|
+
}
|
|
67
|
+
}
|