|
| 1 | +// Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:typed_data'; |
| 7 | + |
| 8 | +import 'package:http/http.dart' as http show readBytes; |
| 9 | +import 'package:meta/meta.dart'; |
| 10 | +import 'dart:html'; |
| 11 | + |
| 12 | +import '../web_helpers/web_helpers.dart'; |
| 13 | +import './base.dart'; |
| 14 | + |
| 15 | +/// A CrossFile that works on web. |
| 16 | +/// |
| 17 | +/// It wraps the bytes of a selected file. |
| 18 | +class XFile extends XFileBase { |
| 19 | + String path; |
| 20 | + |
| 21 | + final String mimeType; |
| 22 | + final Uint8List _data; |
| 23 | + final int _length; |
| 24 | + final String name; |
| 25 | + final DateTime _lastModified; |
| 26 | + Element _target; |
| 27 | + |
| 28 | + final CrossFileTestOverrides _overrides; |
| 29 | + |
| 30 | + bool get _hasTestOverrides => _overrides != null; |
| 31 | + |
| 32 | + /// Construct a CrossFile object from its ObjectUrl. |
| 33 | + /// |
| 34 | + /// Optionally, this can be initialized with `bytes` and `length` |
| 35 | + /// so no http requests are performed to retrieve files later. |
| 36 | + /// |
| 37 | + /// `name` needs to be passed from the outside, since we only have |
| 38 | + /// access to it while we create the ObjectUrl. |
| 39 | + XFile( |
| 40 | + this.path, { |
| 41 | + this.mimeType, |
| 42 | + this.name, |
| 43 | + int length, |
| 44 | + Uint8List bytes, |
| 45 | + DateTime lastModified, |
| 46 | + @visibleForTesting CrossFileTestOverrides overrides, |
| 47 | + }) : _data = bytes, |
| 48 | + _length = length, |
| 49 | + _overrides = overrides, |
| 50 | + _lastModified = lastModified, |
| 51 | + super(path); |
| 52 | + |
| 53 | + /// Construct an CrossFile from its data |
| 54 | + XFile.fromData( |
| 55 | + Uint8List bytes, { |
| 56 | + this.mimeType, |
| 57 | + this.name, |
| 58 | + int length, |
| 59 | + DateTime lastModified, |
| 60 | + this.path, |
| 61 | + @visibleForTesting CrossFileTestOverrides overrides, |
| 62 | + }) : _data = bytes, |
| 63 | + _length = length, |
| 64 | + _overrides = overrides, |
| 65 | + _lastModified = lastModified, |
| 66 | + super(path) { |
| 67 | + if (path == null) { |
| 68 | + final blob = (mimeType == null) ? Blob([bytes]) : Blob([bytes], mimeType); |
| 69 | + this.path = Url.createObjectUrl(blob); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @override |
| 74 | + Future<DateTime> lastModified() async { |
| 75 | + if (_lastModified != null) { |
| 76 | + return Future.value(_lastModified); |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + Future<Uint8List> get _bytes async { |
| 82 | + if (_data != null) { |
| 83 | + return Future.value(UnmodifiableUint8ListView(_data)); |
| 84 | + } |
| 85 | + return http.readBytes(path); |
| 86 | + } |
| 87 | + |
| 88 | + @override |
| 89 | + Future<int> length() async { |
| 90 | + return _length ?? (await _bytes).length; |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + Future<String> readAsString({Encoding encoding = utf8}) async { |
| 95 | + return encoding.decode(await _bytes); |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + Future<Uint8List> readAsBytes() async { |
| 100 | + return Future.value(await _bytes); |
| 101 | + } |
| 102 | + |
| 103 | + @override |
| 104 | + Stream<Uint8List> openRead([int start, int end]) async* { |
| 105 | + final bytes = await _bytes; |
| 106 | + yield bytes.sublist(start ?? 0, end ?? bytes.length); |
| 107 | + } |
| 108 | + |
| 109 | + /// Saves the data of this CrossFile at the location indicated by path. |
| 110 | + /// For the web implementation, the path variable is ignored. |
| 111 | + void saveTo(String path) async { |
| 112 | + // Create a DOM container where we can host the anchor. |
| 113 | + _target = ensureInitialized('__x_file_dom_element'); |
| 114 | + |
| 115 | + // Create an <a> tag with the appropriate download attributes and click it |
| 116 | + // May be overridden with CrossFileTestOverrides |
| 117 | + final AnchorElement element = |
| 118 | + (_hasTestOverrides && _overrides.createAnchorElement != null) |
| 119 | + ? _overrides.createAnchorElement(this.path, this.name) |
| 120 | + : createAnchorElement(this.path, this.name); |
| 121 | + |
| 122 | + // Clear the children in our container so we can add an element to click |
| 123 | + _target.children.clear(); |
| 124 | + addElementToContainerAndClick(_target, element); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Overrides some functions to allow testing |
| 129 | +@visibleForTesting |
| 130 | +class CrossFileTestOverrides { |
| 131 | + /// For overriding the creation of the file input element. |
| 132 | + Element Function(String href, String suggestedName) createAnchorElement; |
| 133 | + |
| 134 | + /// Default constructor for overrides |
| 135 | + CrossFileTestOverrides({this.createAnchorElement}); |
| 136 | +} |
0 commit comments