1- /* global MAXFILESIZE */
2- /* global DEFAULT_EXPIRE_SECONDS */
1+ /* global DEFAULTS LIMITS */
32import FileSender from './fileSender' ;
43import FileReceiver from './fileReceiver' ;
54import { copyToClipboard , delay , openLinksInNewTab , percent } from './utils' ;
65import * as metrics from './metrics' ;
7- import { hasPassword } from './api' ;
86import Archive from './archive' ;
97import { bytes } from './utils' ;
8+ import { prepareWrapKey } from './fxa' ;
109
1110export default function ( state , emitter ) {
1211 let lastRender = 0 ;
@@ -17,19 +16,8 @@ export default function(state, emitter) {
1716 }
1817
1918 async function checkFiles ( ) {
20- const files = state . storage . files . slice ( ) ;
21- let rerender = false ;
22- for ( const file of files ) {
23- const oldLimit = file . dlimit ;
24- const oldTotal = file . dtotal ;
25- await file . updateDownloadCount ( ) ;
26- if ( file . dtotal === file . dlimit ) {
27- state . storage . remove ( file . id ) ;
28- rerender = true ;
29- } else if ( oldLimit !== file . dlimit || oldTotal !== file . dtotal ) {
30- rerender = true ;
31- }
32- }
19+ const changes = await state . user . syncFileList ( ) ;
20+ const rerender = changes . incoming || changes . downloadCount ;
3321 if ( rerender ) {
3422 render ( ) ;
3523 }
@@ -57,6 +45,16 @@ export default function(state, emitter) {
5745 lastRender = Date . now ( ) ;
5846 } ) ;
5947
48+ emitter . on ( 'login' , async ( ) => {
49+ const k = await prepareWrapKey ( state . storage ) ;
50+ location . assign ( `/api/fxa/login?keys_jwk=${ k } ` ) ;
51+ } ) ;
52+
53+ emitter . on ( 'logout' , ( ) => {
54+ state . user . logout ( ) ;
55+ render ( ) ;
56+ } ) ;
57+
6058 emitter . on ( 'changeLimit' , async ( { file, value } ) => {
6159 await file . changeLimit ( value ) ;
6260 state . storage . writeFile ( file ) ;
@@ -90,29 +88,37 @@ export default function(state, emitter) {
9088 } ) ;
9189
9290 emitter . on ( 'addFiles' , async ( { files } ) => {
93- if ( state . archive ) {
94- if ( ! state . archive . addFiles ( files ) ) {
95- // eslint-disable-next-line no-alert
96- alert ( state . translate ( 'fileTooBig' , { size : bytes ( MAXFILESIZE ) } ) ) ;
97- return ;
98- }
99- } else {
100- const archive = new Archive ( files ) ;
101- if ( ! archive . checkSize ( ) ) {
102- // eslint-disable-next-line no-alert
103- alert ( state . translate ( 'fileTooBig' , { size : bytes ( MAXFILESIZE ) } ) ) ;
104- return ;
105- }
106- state . archive = archive ;
91+ const maxSize = state . user . maxSize ;
92+ state . archive = state . archive || new Archive ( ) ;
93+ try {
94+ state . archive . addFiles ( files , maxSize ) ;
95+ } catch ( e ) {
96+ alert (
97+ state . translate ( e . message , {
98+ size : bytes ( maxSize ) ,
99+ count : LIMITS . MAX_FILES_PER_ARCHIVE
100+ } )
101+ ) ;
107102 }
108103 render ( ) ;
109104 } ) ;
110105
111106 emitter . on ( 'upload' , async ( { type, dlCount, password } ) => {
112107 if ( ! state . archive ) return ;
108+ if ( state . storage . files . length >= LIMITS . MAX_ARCHIVES_PER_USER ) {
109+ return alert (
110+ state . translate ( 'tooManyArchives' , {
111+ count : LIMITS . MAX_ARCHIVES_PER_USER
112+ } )
113+ ) ;
114+ }
113115 const size = state . archive . size ;
114- if ( ! state . timeLimit ) state . timeLimit = DEFAULT_EXPIRE_SECONDS ;
115- const sender = new FileSender ( state . archive , state . timeLimit ) ;
116+ if ( ! state . timeLimit ) state . timeLimit = DEFAULTS . EXPIRE_SECONDS ;
117+ const sender = new FileSender (
118+ state . archive ,
119+ state . timeLimit ,
120+ state . user . bearerToken
121+ ) ;
116122
117123 sender . on ( 'progress' , updateProgress ) ;
118124 sender . on ( 'encrypting' , render ) ;
@@ -132,7 +138,6 @@ export default function(state, emitter) {
132138 metrics . completedUpload ( ownedFile ) ;
133139
134140 state . storage . addFile ( ownedFile ) ;
135-
136141 if ( password ) {
137142 emitter . emit ( 'password' , { password, file : ownedFile } ) ;
138143 }
@@ -185,17 +190,6 @@ export default function(state, emitter) {
185190 render ( ) ;
186191 } ) ;
187192
188- emitter . on ( 'getPasswordExist' , async ( { id } ) => {
189- try {
190- state . fileInfo = await hasPassword ( id ) ;
191- render ( ) ;
192- } catch ( e ) {
193- if ( e . message === '404' ) {
194- return emitter . emit ( 'pushState' , '/404' ) ;
195- }
196- }
197- } ) ;
198-
199193 emitter . on ( 'getMetadata' , async ( ) => {
200194 const file = state . fileInfo ;
201195
@@ -204,7 +198,7 @@ export default function(state, emitter) {
204198 await receiver . getMetadata ( ) ;
205199 state . transfer = receiver ;
206200 } catch ( e ) {
207- if ( e . message === '401' ) {
201+ if ( e . message === '401' || e . message === '404' ) {
208202 file . password = null ;
209203 if ( ! file . requiresPassword ) {
210204 return emitter . emit ( 'pushState' , '/404' ) ;
0 commit comments