Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: example/src/examples/file_repository.dart

Issue 930513002: Added README, a bin/serve example server which can be used locally. (Closed) Base URL: https://github.com/dart-lang/pub_server.git@master
Patch Set: Moved to old-style async due to another mimepart stream issue, small bugfix' Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library appengine_pub.file_repository;
6
7 import 'dart:async';
8 import 'dart:convert';
9 import 'dart:io';
10
11 import 'package:archive/archive.dart';
12 import 'package:logging/logging.dart';
13 import 'package:path/path.dart' as path;
14 import 'package:pub_server/repository.dart';
15 import 'package:yaml/yaml.dart';
16
17 final Logger _logger = new Logger('pub_server.file_repository');
18
19 /// Implements the [PackageRepository] by storing pub packages on a file system.
20 class FileRepository extends PackageRepository {
21 final String baseDir;
22
23 FileRepository(this.baseDir);
24
25 Stream<PackageVersion> versions(String package) {
26 var directory = new Directory(path.join(baseDir, package));
27 if (directory.existsSync()) {
28 return directory
29 .list(recursive: false)
30 .where((fse) => fse is Directory)
31 .map((Directory dir) {
32 var version = path.basename(dir.path);
33 var pubspecFile = new File(pubspecFilePath(package, version));
34 var tarballFile = new File(packageTarballPath(package, version));
35 if (pubspecFile.existsSync() && tarballFile.existsSync()) {
36 var pubspec = pubspecFile.readAsStringSync();
37 return new PackageVersion(package, version, pubspec);
38 }
39 });
40 }
41
42 return new Stream.fromIterable([]);
43 }
44
45 // TODO: Could be optimized by searching for the exact package/version
46 // combination instead of enumerating all.
47 Future<PackageVersion> lookupVersion(String package, String version) {
48 return versions(package)
49 .where((pv) => pv.versionString == version)
50 .toList().then((List<PackageVersion> versions) {
51 if (versions.length >= 1) return versions.first;
52 return null;
53 });
54 }
55
56 bool get supportsUpload => true;
57
58 Future upload(Stream<List<int>> data) {
59 _logger.info('Start uploading package.');
60 return data.fold(new BytesBuilder(), (b, d) => b..add(d)).then((bb) {
61 var tarballBytes = bb.takeBytes();
62 var tarBytes = new GZipDecoder().decodeBytes(tarballBytes);
63 var archive = new TarDecoder().decodeBytes(tarBytes);
64 var pubspecArchiveFile;
65 for (var file in archive.files) {
66 if (file.name == 'pubspec.yaml') {
67 pubspecArchiveFile = file;
68 break;
69 }
70 }
71 if (pubspecArchiveFile != null) {
72 // TODO: Error handling.
73 var pubspec = loadYaml(UTF8.decode(pubspecArchiveFile.content));
74
75 var package = pubspec['name'];
76 var version = pubspec['version'];
77
78 var packageVersionDir =
79 new Directory(path.join(baseDir, package, version));
80 var pubspecFile = new File(pubspecFilePath(package, version));
81 var tarballFile = new File(packageTarballPath(package, version));
82
83 if (!packageVersionDir.existsSync()) {
84 packageVersionDir.createSync(recursive: true);
85 }
86 pubspecFile.writeAsBytesSync(pubspecArchiveFile.content);
87 tarballFile.writeAsBytesSync(tarballBytes);
88
89 _logger.info('Uploaded new $package/$version');
90 } else {
91 _logger.warning('Did not find any pubspec.yaml file in upload. '
92 'Aborting.');
93 throw 'No pubspec file.';
94 }
95 });
96 }
97
98 bool get supportsDownloadUrl => false;
99
100 Future<Stream> download(String package, String version) {
101 var pubspecFile = new File(pubspecFilePath(package, version));
102 var tarballFile = new File(packageTarballPath(package, version));
103
104 if (pubspecFile.existsSync() && tarballFile.existsSync()) {
105 return new Future.value(tarballFile.openRead());
106 } else {
107 return new Future.error(
108 'package cannot be downloaded, because it does not exist');
109 }
110 }
111
112 String pubspecFilePath(String package, String version)
113 => path.join(baseDir, package, version, 'pubspec.yaml');
114
115 String packageTarballPath(String package, String version)
116 => path.join(baseDir, package, version, 'package.tar.gz');
117 }
OLDNEW
« no previous file with comments | « example/src/examples/cow_repository.dart ('k') | example/src/examples/http_proxy_repository.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698