1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 19:42:01 +02:00

Merge pull request #89 from codeskyblue/fs-pr

Add filesystem support.
This commit is contained in:
Simo Kinnunen 2015-09-27 20:51:36 +09:00
commit f6fb8abee4
13 changed files with 267 additions and 0 deletions

View file

@ -38,6 +38,7 @@ module.exports = function(options) {
.dependency(require('./plugins/ringer'))
.dependency(require('./plugins/wifi'))
.dependency(require('./plugins/sd'))
.dependency(require('./plugins/filesystem'))
.define(function(options, heartbeat, solo) {
if (process.send) {
// Only if we have a parent process

View file

@ -0,0 +1,82 @@
var util = require('util')
var syrup = require('stf-syrup')
var adbkit = require('adbkit')
var path = require('path')
var logger = require('../../../util/logger')
var wire = require('../../../wire')
var wireutil = require('../../../wire/util')
module.exports = syrup.serial()
.dependency(require('../support/adb'))
.dependency(require('../support/router'))
.dependency(require('../support/push'))
.dependency(require('../support/storage'))
.dependency(require('../resources/minicap'))
.dependency(require('./util/display'))
.define(function(options, adb, router, push, storage, minicap, display) {
var log = logger.createLogger('device:plugins:filesystem')
var plugin = Object.create(null)
plugin.retrieve = function(file) {
log.info('Retriving file %s', file)
return adb.stat(options.serial, file)
.then(function(stats){
if (stats.size == 0){
log.info(util.format("File %s is empty", file))
}
return adb.pull(options.serial, file)
.then(function(transfer){
// if this is a new store type, somethings need add to units/storage/plugins/
return storage.store('blob', transfer, {
filename: path.basename(file),
contentType: 'application/octet-stream', // FIXME(ssx): need to detect file type
knownLength: stats.size
})
})
})
.finally(function(){
log.info(util.format("File %s transfer finished", file))
})
}
router.on(wire.FileSystemGetMessage, function(channel, message) {
var reply = wireutil.reply(options.serial)
plugin.retrieve(message.file)
.then(function(file){
push.send([
channel,
reply.okay('success', file)
])
})
.catch(function(err){
log.error('File retrieve %s failed\n%s', message.file, err.stack)
push.send([
channel,
reply.fail(err.message)
])
})
})
router.on(wire.FileSystemListMessage, function(channel, message){
var reply = wireutil.reply(options.serial)
adb.readdir(options.serial, message.dir)
.then(function(files){
push.send([
channel,
reply.okay('success', files)
])
})
.catch(function(err){
log.error('Dir list %s failed\n%s', message.dir, err.stack)
push.send([
channel,
reply.fail(err.message)
])
})
})
return plugin;
})

View file

@ -138,6 +138,10 @@ module.exports = function(options) {
app.get('/s/blob/:id/:name', function(req, res) {
var file = storage.retrieve(req.params.id)
if (file) {
if (typeof req.query.download !== 'undefined') {
res.set('Content-Disposition',
'attachment; filename="' + path.basename(file.name) + '"')
}
res.set('Content-Type', file.type)
res.sendFile(file.path)
}

View file

@ -826,6 +826,26 @@ module.exports = function(options) {
)
])
})
.on('fs.retrieve', function(channel, responseChannel, data) {
joinChannel(responseChannel)
push.send([
channel
, wireutil.transaction(
responseChannel
, new wire.FileSystemGetMessage(data)
)
])
})
.on('fs.list', function(channel, responseChannel, data){
joinChannel(responseChannel)
push.send([
channel
, wireutil.transaction(
responseChannel
, new wire.FileSystemListMessage(data)
)
])
})
})
.finally(function() {
// Clean up all listeners and subscriptions

View file

@ -72,6 +72,16 @@ enum MessageType {
AccountRemoveMessage = 55;
SdStatusMessage = 61;
ReverseForwardsEvent = 72;
FileSystemListMessage = 81;
FileSystemGetMessage = 82;
}
message FileSystemListMessage {
required string dir = 1;
}
message FileSystemGetMessage {
required string file = 1;
}
message Envelope {

View file

@ -225,6 +225,18 @@ module.exports = function ControlServiceFactory(
return sendTwoWay('screen.capture')
}
this.fsretrieve = function(file){
return sendTwoWay('fs.retrieve', {
file: file,
})
}
this.fslist = function(dir){
return sendTwoWay('fs.list', {
dir: dir,
})
}
this.checkAccount = function(type, account) {
return sendTwoWay('account.check', {
type: type

View file

@ -22,6 +22,12 @@ module.exports =
templateUrl: 'control-panes/advanced/advanced.jade',
filters: ['native', 'web']
},
{
title: gettext('FileSystem'),
icon: 'fa-info color-orange',
templateUrl: 'control-panes/filesystem/fs.jade',
filters: ['native', 'web']
},
{
title: gettext('Info'),
icon: 'fa-info color-orange',

View file

@ -0,0 +1,47 @@
module.exports = function FsCtrl($scope) {
$scope.search = ''
$scope.files = [];
$scope.paths = [];
var listdir = function(){
var path = '/'+$scope.paths.join('/');
$scope.control.fslist(path)
.then(function(result){
$scope.files = result.body;
$scope.$digest();
})
.catch(function(err){
alert(err.message);
})
}
$scope.dirEnter = function(name){
if (name){
$scope.paths.push(name);
}
listdir();
$scope.search = '';
}
$scope.dirJump = function(){
if ($scope.paths.length !== 0){
$scope.paths.pop();
}
listdir();
}
$scope.getFile = function(file){
var path = '/'+$scope.paths.join('/')+'/'+file;
$scope.control.fsretrieve(path)
.then(function(result){
location.href = result.body.href+"?download"
})
.catch(function(err){
alert(err.message);
})
}
// init
listdir($scope.dir);
}

View file

@ -0,0 +1,17 @@
describe('FsCtrl', function () {
beforeEach(angular.mock.module(require('./').name));
var scope, ctrl;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('FsCtrl', {$scope: scope});
}));
it('should ...', inject(function () {
expect(1).toEqual(1);
}));
});

View file

@ -0,0 +1,30 @@
.stf-fs(ng-controller='FsCtrl')
.row: .col-md-6
span /{{paths.join("/")}}
button.btn.btn-xm.btn-primary-outline(ng-click='dirJump()')
span(translate) Back
form.input-group.form-inline(ng-submit='dirEnter(search)')
input(type=text, ng-model='search', placeholder="dir or file ...").form-control
span.input-group-btn
button.btn.btn-default.btn-primary-outline(type='submit')
i.fa.fa-search
span(translate) Enter
table.table
tr(ng-repeat='f in files | filter:search | orderBy: "-mode|isdir"')
td
span
i.fa.fa-folder-open(ng-show='f.mode|isdir')
i.fa.fa-file-o(ng-hide='f.mode|isdir')
td
button.btn.btn-sm.btn-primary-outline(
ng-click='dirEnter(f.name)', ng-show='f.mode|isdir')
{{f.name}}
button.btn.btn-sm.btn-primary-outline(
ng-click='getFile(f.name)', ng-hide='f.mode|isdir')
{{f.name}}
td
i {{f.mode|mode2unix}}
td {{f.size}}

View file

View file

@ -0,0 +1,37 @@
require('./fs.less')
module.exports = angular.module('stf.filesystem', [])
.run(["$templateCache", function ($templateCache) {
$templateCache.put('control-panes/filesystem/fs.jade',
require('./fs.jade')
)
}])
.filter('mode2unix', function(){
return function(mode){
if(mode !== null){
var res = [];
var s = ['x', 'w', 'r'];
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if ((mode >> (i*3+j)) & 1 !== 0){
res.unshift(s[j])
} else {
res.unshift('-')
}
}
}
res.unshift(mode & 040000 ? 'd' : '-');
return res.join('');
}
}
})
.filter('isdir', function(){
return function(mode){
if(mode !== null){
mode = parseInt(mode, 10)
mode = mode - (mode & 0777);
return (mode == 040000) || (mode == 0120000);
}
}
})
.controller('FsCtrl', require('./fs-controller'))

View file

@ -13,6 +13,7 @@ module.exports = angular.module('control-panes', [
require('./logs').name,
//require('./resources').name,
require('./screenshots').name,
require('./filesystem').name,
require('./info').name
])
.config(['$routeProvider', function ($routeProvider) {