fix: skip pdfrx cache initialization folder on web platforms
This commit is contained in:
parent
741decdae3
commit
f7d37517a4
|
|
@ -2,32 +2,15 @@ import 'package:flutter/foundation.dart';
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:pdfrx/pdfrx.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'dart:io';
|
||||
import 'package:pdf_signature/app.dart';
|
||||
import 'package:pdf_signature/utils/pdfrx_cache_init/pdfrx_cache_init.dart';
|
||||
export 'package:pdf_signature/app.dart';
|
||||
|
||||
Future<void> _initPdfrxCache() async {
|
||||
try {
|
||||
// Only set once; guard for hot reload/tests
|
||||
if (Pdfrx.getCacheDirectory == null) {
|
||||
final dir = await getTemporaryDirectory();
|
||||
final cacheDir = Directory('${dir.path}/pdfrx_cache');
|
||||
if (!await cacheDir.exists()) {
|
||||
await cacheDir.create(recursive: true);
|
||||
}
|
||||
Pdfrx.getCacheDirectory = () async => cacheDir.path;
|
||||
debugPrint('[main] Pdfrx cache directory set to ${cacheDir.path}');
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint('[main] Failed to initialize Pdfrx cache directory: $e');
|
||||
debugPrint(st.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await _initPdfrxCache();
|
||||
// Initialize pdfrx core (safe to call multiple times) and set up cache directory.
|
||||
pdfrxFlutterInitialize();
|
||||
await initPdfrxCache();
|
||||
// Disable right-click context menu on web using Flutter API
|
||||
if (kReleaseMode) {
|
||||
debugPrint = (String? message, {int? wrapWidth}) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
// ignore_for_file: unnecessary_import
|
||||
import 'dart:typed_data';
|
||||
import 'package:cross_file/cross_file.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
|
@ -10,6 +11,7 @@ import 'package:pdfrx/pdfrx.dart';
|
|||
import 'package:file_picker/file_picker.dart' as fp;
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class PdfViewModel extends ChangeNotifier {
|
||||
final Ref ref;
|
||||
|
|
@ -322,10 +324,8 @@ class PdfSessionViewModel extends ChangeNotifier {
|
|||
int pageCount = 1; // default
|
||||
try {
|
||||
// Defensive: ensure Pdfrx cache directory set (in case main init skipped in tests)
|
||||
if (Pdfrx.getCacheDirectory == null) {
|
||||
debugPrint(
|
||||
'[PdfSessionViewModel] Pdfrx.getCacheDirectory was null; setting temp directory',
|
||||
);
|
||||
if (Pdfrx.getCacheDirectory == null && !kIsWeb) {
|
||||
debugPrint('[PdfSessionViewModel] Setting Pdfrx cache directory (io)');
|
||||
try {
|
||||
final temp = await getTemporaryDirectory();
|
||||
Pdfrx.getCacheDirectory = () async => temp.path;
|
||||
|
|
@ -336,6 +336,8 @@ class PdfSessionViewModel extends ChangeNotifier {
|
|||
debugPrint(st.toString());
|
||||
}
|
||||
}
|
||||
// Ensure engine initialized (safe multiple calls)
|
||||
pdfrxFlutterInitialize();
|
||||
final doc = await PdfDocument.openData(bytes);
|
||||
pageCount = doc.pages.length;
|
||||
debugPrint(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
// Conditional export: use IO implementation except when compiling for web (html).
|
||||
export 'pdfrx_cache_init_io.dart'
|
||||
if (dart.library.html) 'pdfrx_cache_init_web.dart';
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:pdfrx/pdfrx.dart';
|
||||
|
||||
/// Initialize Pdfrx cache directory for IO platforms (mobile/desktop). No-op on web.
|
||||
Future<void> initPdfrxCache() async {
|
||||
try {
|
||||
if (kIsWeb) return; // Guard (should not be used on web, but extra safety)
|
||||
if (Pdfrx.getCacheDirectory != null) return; // Already set
|
||||
final dir = await getTemporaryDirectory();
|
||||
final cacheDir = Directory('${dir.path}/pdfrx_cache');
|
||||
if (!await cacheDir.exists()) {
|
||||
await cacheDir.create(recursive: true);
|
||||
}
|
||||
Pdfrx.getCacheDirectory = () async => cacheDir.path;
|
||||
debugPrint(
|
||||
'[pdfrx_cache_init_io] Pdfrx cache directory set to ${cacheDir.path}',
|
||||
);
|
||||
} catch (e, st) {
|
||||
debugPrint(
|
||||
'[pdfrx_cache_init_io] Failed to initialize Pdfrx cache directory: $e',
|
||||
);
|
||||
debugPrint(st.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:pdfrx/pdfrx.dart';
|
||||
|
||||
/// Web stub: pdfrx can operate without a filesystem cache; leave getCacheDirectory null.
|
||||
Future<void> initPdfrxCache() async {
|
||||
// Intentionally no-op. If desired, could set an in-memory indicator.
|
||||
debugPrint(
|
||||
'[pdfrx_cache_init_web] Skipping Pdfrx cache directory setup on web',
|
||||
);
|
||||
// Ensure any previous (hot-reload) IO assignment isn't kept when switching target.
|
||||
if (kIsWeb && Pdfrx.getCacheDirectory != null) {
|
||||
// Leave as-is; clearing could break existing references. Merely log.
|
||||
debugPrint(
|
||||
'[pdfrx_cache_init_web] Existing getCacheDirectory left unchanged',
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue